最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove select option with specific value - Stack Overflow

programmeradmin1浏览0评论

How can I remove <option value="0">Value 0</option> from the <select> menu based on the value 0?

<select id="someid">
  <option value="0">Value 0</option>
  <option value="1">Value 1</option>
</select>

How can I remove <option value="0">Value 0</option> from the <select> menu based on the value 0?

<select id="someid">
  <option value="0">Value 0</option>
  <option value="1">Value 1</option>
</select>
Share Improve this question edited Sep 11, 2021 at 19:10 Sebastian Simon 19.5k8 gold badges61 silver badges84 bronze badges asked Dec 13, 2016 at 1:59 user1240207user1240207 2171 gold badge3 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You don't need Prototype JS to do this.

Removing an option by index:

var select = document.getElementById('someid')
select.removeChild(select.options[0])
<select id='someid'>
    <option value='0'>Value 0</option>
    <option value='1'>Value 1</option>
</select>

Removing an option with a specific value:

var select = document.getElementById('someid')
select.removeChild(getOptionByValue(select, '0'))

function getOptionByValue (select, value) {
    var options = select.options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].value === value) {
            return options[i]  
        }
    }
    return null
}
<select id='someid'>
    <option value='0'>Value 0</option>
    <option value='1'>Value 1</option>
</select>

Or, inspired by this answer:

(Edit: RobG submitted this technique before me; all credit hereafter to him. I saw his ment critiquing my answer and began editing my post accordingly, but didn't scroll down far enough to see his answer as well, which had addressed that issue already.)

var select = document.getElementById('someid')
select.removeChild(select.querySelector('option[value="0"]'))
<select id='someid'>
    <option value='0'>Value 0</option>
    <option value='1'>Value 1</option>
</select>

You can use a selector to get an option with a specific value, then remove it. The following uses querySelector, but you could also loop over all the options and find the one(s) with the required value and remove them the same way.

function removeOption() {
  var optValue = document.getElementById('i0').value;
  var errorEl = document.getElementById('errorMessage');
  var optElement = document.querySelector('option[value="' + optValue + '"]');
  if (optElement) {
    errorEl.textContent = '';
    optElement.parentNode.removeChild(optElement);
  } else {
    errorEl.textContent = 'There is no option with value "' + optValue + '"';
  }
}
#errorMessage {
  background-color: white;
  color: red;
}
<select id='someid'>
    <option value='0'>Value 0</option>
    <option value='1'>Value 1</option>
</select>
<br>
Remove option with value: 
  <input id="i0">
  <button onclick="removeOption()">Remove option</button>
  <br>
  <span id="errorMessage"></span>

发布评论

评论列表(0)

  1. 暂无评论