简体   繁体   English

我如何使用 php、javascript 从列表框中删除选定的项目?

[英]How i can i delete a selected item out of a listbox using php,javascript?

I got 2 buttons.我有2个按钮。 One is to add an item to the listbox.一种是将项目添加到列表框。 The other is to delete something out of the listbox.另一种是从列表框中删除某些内容。 The filling happens with an array.填充发生在数组中。 So what I need to know is: how can i delete a selected item out of the listbox?所以我需要知道的是:如何从列表框中删除选定的项目?

Thanks Stijn谢谢斯蒂恩

<script type='text/javascript'>
function removeItem(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
if(selectbox.options[i].selected)
selectbox.remove(i);
}
}
</script>

<SELECT id="item" NAME="item" MULTIPLE size=6 width=10></SELECT>
<input type=button onClick="removeItem(item)"; value='Remove Selected Item'>

You can delete one by one using this function.您可以使用此功能一一删除。

<select id="list">
   <option>A</option>
   <option>B</option>
   <option>C</option>
</select>

<button onclick="remove();">Remove</button>

<script>
function remove () 
{
   var list = document.getElementById('list');
   list[list.selectedIndex].remove();
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM