简体   繁体   中英

How to select(fetch) all the <select> elements in jQuery

I want to fill in all the options for things like

<select id=such_a_field>

to get the inputs in my form I can just go

$inputs = $('#myFormName :input');

and iterate through them and set the value based on the id. For my select field, which I am now upgrading from a simple input to restrict what the user can put in, I first want to go read the set of available options (from the DB, via ajax of course), and then set the right one based on what's in the DB. Unfortunately

$selects = $('#myFormName :select');

produces

Syntax error, unrecognized expression: unsupported pseudo: select

as of course :select isnt a pseudo selector. It would be enough just to extend my above :input selector and then enhance my field setting code to handle selects.

You are looking for

$('#myFormName select')

select is a tag, so you must query for it as it is. :selected will give you the selected option s.

您需要使用元素选择器

$selects = $('#myFormName select');

您想要表单中所有选择标签输入的集合,请执行以下操作:-

$selects = $('#myFormName select');

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