开发者

checkbox value in jquery

开发者 https://www.devze.com 2022-12-08 07:50 出处:网络
i\'m using Mvc checkbox. by default the rendering a checkbox like below. <input id=\"tets\" type=\"checkbo开发者_JAVA百科x\" value=\"true\" name=\"test\"/>

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

0

精彩评论

暂无评论...
验证码 换一张
取 消