Suppose a list of options is available, how do you update the <select>
with new <option>
s?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
Does this help?
where comboBx is your combo box id.
or you can append options as string to the already existing innerHTML and then assign to the select innerHTML.
Edit
If you need to keep the first option and remove all other then you can use
Removing and adding DOM element is slower than modification of existing one.
If your option sets have same length, you may do something like this:
In case your new option set has different length, you may delete/add empty options you really need, using some technique described above.
You can remove the existing options by using the
empty
method, and then add your new options:If you have your new options in an object you can:
Edit: For removing the all the options but the first, you can use the
:gt
selector, to get all theoption
elements with index greater than zero andremove
them:I threw CMS's excellent answer into a quick jQuery extension:
It expects an array of objects which contain "text" and "value" keys. So usage is as follows:
If for example your html code contain this code:
In order to change the list of option inside your select, you can use this code bellow. when your name select named selectId.
in this example above i change the old list of option by only one new option.