在 optgroup 中选择选择器添加选项 [英] Selectpicker add option in optgroup
本文介绍了在 optgroup 中选择选择器添加选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 jQuery 在 optgroup
中添加一个选项.我正在使用引导程序选择器.以下是我的代码:
I want to add an option in an optgroup
using jQuery. I am using bootstrap selectpicker. The following is my code:
var groupOption = "<option value='123'>123</option>";
$("#selectpickerdemo").append(groupOption).selectpicker('refresh');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selectpicker" multiple name="selectpickerdemo" id="selectpickerdemo">
<optgroup label="Label 1">
<option value='abc'>ABC</option>
<option value='abcd'>ABCD</option>
</optgroup>
<optgroup label="Label 2">
<option value='abc'>ABC</option>
<option value='abcd'>ABCD</option>
</optgroup>
</select>
推荐答案
请使用 id
属性(或其他类似 class
等).例如
Please use id
attribute (or others like class
, etc).
e.g.
...
<optgroup label="Label 1" id="label1">
<option value='abc'>ABC</option>
<option value='abcd'>ABCD</option>
</optgroup>
...
var opt_label1 = $("#label1" );
$("<option>").text('option text').val('option value').appendTo(opt_label1);
更多细节,请参考这个例子.http://jsfiddle.net/jonathansampson/493qa/
For more details, please refer to this example. http://jsfiddle.net/jonathansampson/493qa/
这篇关于在 optgroup 中选择选择器添加选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文