I’ve read a few articles talking about checkboxes always returning a false state, but haven’t found anything about my own problem.
So, here is the script:
<script type="text/javascript">
function update_contact(id, name) {
alert("idCont : " + id + "\n nameCKB : " + name + "\n state : "
+ $(this).attr('checked'));
var a = location.pathname.substring(1).split('/')
$.ajax({
url : '@Url.Action("update_contact")',
type : 'POST',
data : {
name : name,
isChecked : $(this).is(':checked'),
idOpp : a[2],
idCont : id
},
success : function(result) {
}
});
};
</script>
And here is the checkbox’s code :
@If mail = False Then
@<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed')" />
Else
@<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed')" checked="checked" />
End If
And here is the code generated by the server :
<input name="mailed" id="mailed" class="mailed" onclick="update_contact(1 , 'mailed')" type="checkbox">
In the beginning, I used a Html helper. It was returning smth like that:
<input id="mailed" name="mailed" onclick="update_contact(1 ,'mailed')" value="true" type="checkbox">
<input name="mailed" value="false" type="hidden">
I though it was due to the second input that it was always returning a false state.
The problem is likely that
thisis not what you think it is. Try explicitly passing a reference to the clicked checkbox to your function:And then:
Alternatively use jQuery to assign the click handler and it will set
thisfor you correctly. You could put the@item.idContact.toString()in thevalueattribute and then access it usingthis.valuein your handler:And then:
(Note: I don’t actually know the VB/razor syntax, I’m just guessing on that part.)