简体   繁体   English

从选择框中删除项目或将项目添加到选择框中?

[英]Remove items from or add items to a select box?

How to remove items (value="0") from select box? 如何从选择框中删除项目(值=“ 0”)?

Below is an example select box. 下面是一个示例选择框。

<select class="form-select" name="column" id="edit-column">
<option value="-1">Search All Columns</option>
<option value="0">option 1</option>
<option value="1">option 2</option>
</select>

my JavaScript is not working 我的JavaScript无法运作

$('#edit-column option[value="0"]').remove();

Your JavaScript is fine -- you're probably just running it before the DOM is fully loaded. 您的JavaScript很好-您可能只是在DOM完全加载之前运行它。

So, as PSL mentioned in the comments, you have to use something like this in jQuery: 因此,正如评论中提到的PSL一样,您必须在jQuery中使用类似以下的内容:

$(function(){
   $('#edit-column option[value="0"]').remove();
});

Which is really just shorthand for: 这实际上只是以下方面的简写:

$(document).ready(function() {
    $('#edit-column option[value="0"]').remove();
});

Here's the updated fiddle . 这是更新的小提琴

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

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