我使用的是chosen js库:
<select class="chosen-select" name="country" id="country" onchange="setCountry('client');">
...
</select>
然后我需要更改这个select元素:
$("#country").append('<option value="'+ countryid +'">' + countryname + '</option>');
但它不能与这个库一起工作,而不能在没有库的情况下工作。我怎么才能修复它?谢谢!
发布于 2018-10-08 18:21:19
$("#form_field").trigger("chosen:updated");
编辑元素后,我必须使用此代码,它可以工作
发布于 2018-10-08 01:44:48
通过使用下面的代码,您可以实现您的目标。
var currentelement = document.querySelector('#country');
var newOptionElement = document.createElement("option");
newOptionElement.text = "Test";
currentelement.add(newOptionElement);
var newOptionElement = document.createElement("option");
newOptionElement.text = "Test1";
currentelement.add(newOptionElement);
var newOptionElement = document.createElement("option");
newOptionElement.text = "Test2";
currentelement.add(newOptionElement);
<select class="chosen-select" name="country" id="country">
</select>
var currentelement = document.querySelector('#country');
var newOptionElement = document.createElement("option");
newOptionElement.text = "Test";
currentelement.add(newOptionElement);
https://stackoverflow.com/questions/52691131
复制相似问题