remove()函数在javascript中不起作用

时间:2022-05-23 19:19:10

I have an html select box and a search field (input type)

我有一个HTML选择框和一个搜索字段(输入类型)

When I Search for something new , a javascript function first clears the selectfield

当我搜索新内容时,javascript函数首先清除selectfield

But Javascript gives the following error:

但Javascript给出以下错误:

gs.options.remove is not a function

gs.options.remove不是一个函数

This is the function

这是功能

 function clearScholen()
  {
    for (var i=gs.options.length;i>=0;i--)
    {
      gs.options.remove(i); 
    }  
  }

and the value of gs =

和gs =的值

<select style="width: 420px; height: 150px;" name="selectbox" size="5">

Whats going wrong?

什么出错了?

2 个解决方案

#1


I guess in your example "gs" doesnt reference a selectbox.

我想在你的例子中“gs”不会引用一个选择框。

Removing all the options

删除所有选项

 function removeAllOptions(selectbox) 
 {
   var i;
   for(i=selectbox.options.length-1;i>=0;i--)
   { 
     selectbox.remove(i); 
   }
 }

Removing Selected Options

删除所选选项

function removeOptions(selectbox)
{
  var i;
  for (i=selectbox.options.length-1;i>=0;i--) 
  {
      if(selectbox.options[i].selected)
          selectbox.remove(i);     
  }
}

#2


If I understand you correctly, you want to clear the search field (which is working) and reset the select drop down.
If that's the case, you want:

如果我理解正确,您要清除搜索字段(正在运行)并重置选择下拉列表。如果是这种情况,您需要:

gs.selectedIndex = -1;

e.g.

function clearScholen()
  {
       gs.selectedIndex = -1;

  }   

assuming that gs is previously defined

假设先前已定义gs

#1


I guess in your example "gs" doesnt reference a selectbox.

我想在你的例子中“gs”不会引用一个选择框。

Removing all the options

删除所有选项

 function removeAllOptions(selectbox) 
 {
   var i;
   for(i=selectbox.options.length-1;i>=0;i--)
   { 
     selectbox.remove(i); 
   }
 }

Removing Selected Options

删除所选选项

function removeOptions(selectbox)
{
  var i;
  for (i=selectbox.options.length-1;i>=0;i--) 
  {
      if(selectbox.options[i].selected)
          selectbox.remove(i);     
  }
}

#2


If I understand you correctly, you want to clear the search field (which is working) and reset the select drop down.
If that's the case, you want:

如果我理解正确,您要清除搜索字段(正在运行)并重置选择下拉列表。如果是这种情况,您需要:

gs.selectedIndex = -1;

e.g.

function clearScholen()
  {
       gs.selectedIndex = -1;

  }   

assuming that gs is previously defined

假设先前已定义gs