javascript - 多次调用 jQuery 函数,仅删除一次

标签 javascript jquery

我有一个函数可以从下拉框中删除除第一个选项之外的所有选项。每次更改另一个下拉框时,此函数都会更新,并根据选择的其他下拉框附加其他元素。我的问题是我总是想保留第一项,但每当我多次调用该函数时它就会将其删除。有什么更好的方法来做到这一点?

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/

相关文章:

javascript - 使用javascript的多个数组的IndexOf数组

javascript - 如何将 socket.io(在 nodejs 中)的事件处理程序绑定(bind)到我自己的范围?

javascript - 在 RegEx 中为一个组获取多个匹配项

javascript - 如何在不导致底部div跳起来的情况下删除顶部div?

javascript - 合并段落

javascript - 如何显示恰好2位的html数字输入的输入值?

javascript - 在 Kotlin 中定义全局函数,我在 html 文件中使用它作为 onclick 回调

javascript - 从background.js操作popup.html的DOM

javascript - 了解 jQuery 中的处理程序

jquery - 使用 jQuery 构建列表