MVC Checkbox always true

In my MVC view, I had checkbox, like so:

@Html.CheckBoxFor(m => m.IsComplete)

A button click event called a jQuery method to pull the value of the checkbox to send it to a Web API call:

$("#IsComplete").val()

The problem was that the checkbox value in jQuery was always true, whether or not it was checked on the view. To correct this, I had to abandon the “val()” method and instead used the “:checked” operator:

   var isChecked = $("input[name='IsComplete']:checked").length;
   var isComplete = false;

   if (isChecked == 1) {
       isComplete = true;
   }
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: