admin管理员组文章数量:1431426
Is it possible to add items to a select (list box) of html/php through jQuery or javascript when a specific action is triggered by another control in jQuery/javascript? (edited by Alex Thomas)
Is it possible to add items to a select (list box) of html/php through jQuery or javascript when a specific action is triggered by another control in jQuery/javascript? (edited by Alex Thomas)
Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Feb 21, 2011 at 12:56 Ahmad FaridAhmad Farid 14.8k45 gold badges98 silver badges138 bronze badges3 Answers
Reset to default 2Yes, attach a suitable handler to the event of your choice. For example if you want to add new items when another control changes, you might use something like this:
$('#controller').change(function() {
$('#child_select').append('<option>New option</option>');
});
Or alternatively, there are jQuery plugins available to make the drop down list dynamic based on the selection of another element.
Yes, you can add items on the client-side. For example:
// Get the raw DOM select element (first use jQuery
// to find it, then get the raw version via [0]
var select = $("select[name=someField]")[0];
// Add the option
select.options[select.options.length] = new Option("Third Option", "3");
Live example
See also: How do you update all options of a select with jquery?
You can use the:
$('select').append('<option value="id" selected="selected">Text</option>');
method, via jQuery. You can call this a number of ways (and get your data via ajax, stored objects, etc).
If you can provide a little more information of what actions you want to trigger this, I can provide a better example.
Hope this helps.
本文标签: Adding items to select (list box) of htmlphp through jQuery or javascriptStack Overflow
版权声明:本文标题:Adding items to select (list box) of htmlphp through jQuery or javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745565943a2663760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论