简体   繁体   中英

How to select a input with name and value

I need to select a radio input with name and value in jquery

In this example how you select element with name SiblingSex and value female

<form action="">
<input type="radio"  name="SiblingSex" value="male">Male<br>
<input type="radio"  name="SiblingSex" value="female">Female


<input type="radio"  name="ParentSex" value="male">Male<br>
<input type="radio"  name="ParentSex" value="female">Female

</form>

i need some thing like

$('input[name="SiblingSex"]' /*GENDER SELECTOR */)

You can add another attribute selector with this:

$('input[name="SiblingSex"][value="female"]').val();

the above line would give you values every time whether it is checked or not.

so if you only want to have the value when it is checked too then add :checked

$('input[name="SiblingSex"][value="female"]:checked').val();

Just have a look on the Demo on my JS Fiddle Code

Shows the two scenario:

1) when you want the value without selecting radio button.


2) when you want value after selecting radio button.

or may be the thing that you want is here::

JS Fiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM