Reputation: 20406
i'm using Mvc checkbox.
by default the rendering a checkbox like below.
<input id="tets" type="checkbox" value="true" name="test"/>
<input type="hidden" value="false" name="test"/>
so whn itry to access
$("#tets").val() returns true, but defaultly it is false.
Any idea how to access checkbox using jquery
Upvotes: 9
Views: 10197
Reputation: 26
A solution that worked for me when selecting by name is:
$('[input[name="test"]')[0].checked
but selecting by id, as per your example:
$('#test').checked
should work also.
My first example was tested in FF and IE
Upvotes: 0
Reputation: 321578
I think you'd have to do it like this:
var value = $('#test:checked').length ? $('#test').val() : $('input[name=test]').eq(1).val();
Or written a different way
var value = $('input[name=test]').eq(!$('#test:checked').length).val();
Upvotes: 0