我正在使用chosen js库:
<select class="chosen-select" name="country" id="country" onchange="setCountry('client');">
...
</select>
然后我需要更改这个选择元素:
$("#country").append('<option value="'+ countryid +'">' + countryname + '</option>');
但是它不适用于该库,并且可以不使用。 我该如何修复它? 谢谢!
最佳答案
通过使用以下代码,您可以实现您的目标。
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);
关于javascript - 选择的js有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691131/