0

in my mvc application i have a checkbox. but dont know why its value is always true. pls help

my view page

<div id="maindiv">
<%: Html.CheckBoxFor(m => m.status)%>
<%: Html.LabelFor(m => m.status)%>
</div>

and the script is here how i am getting the value TRUE always

<script type="text/javascript">
   $('#status').change(function () {
       alert(" active " + $('#status').val());
   });
</script>

3 Answers 3

5

use instead:

var status = ( $("#status").attr("checked") ? 'checked' : 'unchecked' );
alert(" active " + status);

Explanation:
you were reading the value of the checkbox which is always true, you need to check whether its checked attribute is checked or unchecked. I used the ternary operator to check check whether it's checked or not You could have also used $("#status").is(":checked") but it is slower.

Sign up to request clarification or add additional context in comments.

1 Comment

thnx for the explanation. :)
0
$('#status').change(function () {
   alert(" active " + this.checked);
});

Comments

0

I recommend this answer by Jab because it works.

var myValue = $("#status").is(":checked");

If checked, myValue = true, else myValue = false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.