简体   繁体   中英

How to get all select elements having their with multiple selection criteria, jquery?

How to get all dropdowns having on the basis of multiple attributes name and value. $('input[name="ABC"][value="-1"]') this expression seems to work, but for drop downs this doesn't seems working.

<select name="department"  />
<option value="-1" selected="selected">Select One</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
</select>
alert($("select[name=department][value=-1]").length);

gives 0 in alert box.

refer to this fiddle. http://jsfiddle.net/8QBH7/

alert($("select[name=department] option[value=-1]").length);

I believe this is the query you're looking for:

$("select[name='department'] option[value=-1]")

See fiddle demo

Edit based on your feedback I think you want to use the selected pseudo class. Fiddle

alert($("select[name=department] :selected").val());
$("select").on("change",function(){
    alert($("select[name=department] :selected").val());
});

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