5

I got a very common question that I want to get the value of a HTML input by jQuery selector with its name and specific attribute like checked. Following is my case:

<input type="radio" name="gender" value="man" checked="checked" />
<input type="radio" name="gender" value="women"/>

I tried the following code:

var gener = $("name='gender':checked=checked").val();

But it didn't return a correct value. Hope somebody gives me help on it. Thanks.

4

3 回答 3

6

You need to type the element then the attribute, like this $('element[attribute="value"]')

$('input[name="gender"]:checked').val();
于 2013-01-22T16:04:33.973 回答
4

With the :checked selector, you don't need to provide a value, try this:

var gender = $('input[name="gender"]:checked').val();

More information in the API docs

于 2013-01-22T16:04:11.310 回答
2
$('input[name="gender"]:checked').val();

Are you looking for something like that?

于 2013-01-22T16:05:14.943 回答