checkboxFor renders the following for a checkbox:
<input id="IsCredit" name="IsCredit" type="checkbox" value="true" />
<input name="IsCredit" type="hidden" value="false" />
In javascript,
$('#IsCredit').val();
is ALWAYS true. Whether ticked or unticked
How can I determine if the checkbox has been ticked in Javascript?
You can check it's checked
property. With jQuery:
var isChecked = $('#IsCredit').prop("checked");
Or with native DOM methods:
var isChecked = document.getElementById("IsCredit").checked;
val
returns a value of a form element, as your element has a true
value, val()
returns true
not it's checked property value, you can use prop
method for reading checked
property of a checkbox, or is
method.
if ( $('#IsCredit').is(':checked') {
// ...
}
or:
var checked = $('#IsCredit').prop('checked');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With