我有一个函数可以从下拉框中删除除第一个选项之外的所有选项。每次更改另一个下拉框时,此函数都会更新,并根据选择的其他下拉框附加其他元素。我的问题是我总是想保留第一项,但每当我多次调用该函数时它就会将其删除。有什么更好的方法来做到这一点?
function updateMountingMethod(){
var selectedVal = $('#attrib-13 option:selected').text().split(" ")[0];
var optionChildren = $("#attrib-7").children(":not(:first-child)").clone();
$("#attrib-7").children(":not(:last-child)").remove();
$.each(optionChildren, function(){
if ($(this).text().split('|')[1]!=undefined)
{
var values = $(this).text().split('|')[1];
if(values.substring(1,3) == selectedVal)
{
$("#attrib-7").append($(this).text($(this).text().split("|")[0]+" "+$(this).text().split("|")[2]));
}
}
})
}
最佳答案
document.getElementByID('attrib-7').options.length = 1;
这将从选择列表中删除除第一个选项之外的所有选项。不确定 JQuery 是否等效。
关于javascript - 多次调用 jQuery 函数,仅删除一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4628841/