9

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

1
  • 1
    Is the "#tets" typo also in your actual code? $("#test").val() should be all you need to access the checkbox value. Commented Oct 6, 2009 at 14:52

3 Answers 3

23
var value = $("#tets").is(":checked");
Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.