i'm using Mvc checkbox.
by default the rendering a checkbox like below.
<input id="tets" type="checkbo开发者_JAVA百科x" 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
var value = $("#tets").is(":checked");
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();
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
精彩评论