简体   繁体   English

使用纯JavaScript禁用基于innerhtml的选择选项

[英]Disable select option based on innerhtml with pure JavaScript

Scenario: User votes via HTML form for a field trip to take on a bus. 场景:用户通过HTML表单投票进行实地考察以乘坐公共汽车。 There are three options: 有三种选择:

    <select id="selectbox">
      <option>Berlin</option>
      <option>Munich</option>
      <option>Cologne</option>
    </select>

The free bus seats are stored in / read from a database: ($tour is our array keeping the free seats) 免费巴士座位存储在数据库中/从数据库中读取:( $ tour是我们保持免费座位的阵列)

<table class="status">
  <tr><td>Berlin: <span id="t1">available (<?php echo $tour[0]; ?> seats)</span></td></tr>
  <tr><td>Munich: <span id="t2">available (<?php echo $tour[1]; ?> seats)</span></td></tr>
  <tr><td>Cologne: <span id="t3">available (<?php echo $tour[2]; ?> seats)</span></td></tr>
</table>

If free seats are zero, we display a "sorry, booked out" info using vanilla JavaScript: 如果免费座位为零,我们会使用vanilla JavaScript显示“抱歉,预订”信息:

// get content of status table
var t1 = document.getElementById("t1").innerHTML;
var t2 = document.getElementById("t2").innerHTML;
var t3 = document.getElementById("t3").innerHTML;
var bookedout = "sorry, booked out!"

    // check if condition is met
if (t1 == "available (0 seats)") {
    document.getElementById("t1").innerHTML = bookedout;
}
if (t2 == "available (0 seats)") {
    document.getElementById("t2").innerHTML = bookedout ;
}
if (t3 == "available (0 seats)") {
    document.getElementById("t3").innerHTML = bookedout ;
}

Works fine. 工作正常。 However, now comes the part I'm a bit lost. 然而,现在我失去了部分。 The above condition should also delete the respective option from #selectbox based on the option's innerHTML . 上述条件还应该根据选项的innerHTML从#selectbox中删除相应的选项。 In jQuery I'd go with something like #selectbox option:contains('stringhere'). 在jQuery中,我会选择#selectbox选项:contains('stringhere')。

However, I wanna do it in the purest of JavaScript. 但是,我想用最纯粹的JavaScript来做。 Any ideas? 有任何想法吗?

It's fairly straight forward.. 这是相当直接的..

First in your html give vlaues to your options: 首先在你的html为你的选择提供赞美:

<select id="selectbox">
  <option>Berlin</option>
  <option>Munich</option>
  <option>Cologne</option>
</select>

Then in js: 然后在js:

var mySelect = document.getElementById("selectbox");
//function to get option values as array
function getOptionsArr() {
var optionsArray =[],options = mySelect.options; 
var i = 0, len = options.length;
// store the options value in an array
while (i < len)
{
  optionsArray.push(options[i++].value);
}
    return optionsArray;
}
var t1 = document.getElementById("t1").innerHTML;
var t2 = document.getElementById("t2").innerHTML;
var t3 = document.getElementById("t3").innerHTML;
var bookedout = "sorry, booked out!"

// check if condition is met
if (t1 == "available (0 seats)"){
    document.getElementById("t1").innerHTML = bookedout;
    //this will get the whole parent node and child node inner text we split at : and get the value
    var textArr = document.getElementById("t1").parentElement.innerText.split(':');
 // find the index of value from our array created above and remove that option from select
 mySelect.remove(getOptionsArr().indexOf(textArr [0]))
}
//repeat the same for other
if (t2 == "available (0 seats)"){
 document.getElementById("t2").innerHTML = bookedout ;
 var textArr = document.getElementById("t2").parentElement.innerText.split(':');
 mySelect.remove(getOptionsArr().indexOf(textArr [0]))
}
if (t3 == "available (0 seats)"){
document.getElementById("t3").innerHTML = bookedout ;
 var textArr = document.getElementById("t3").parentElement.innerText.split(':');
 mySelect.remove(getOptionsArr().indexOf(textArr [0]))
}

Aditionally you can refactor it by 你可以通过它重构它

var mySelect = document.getElementById("selectbox");
//function to get option values as array
function getOptionsArr() {
var optionsArray =[],options = mySelect.options; 
var i = 0, len = options.length;
// store the options value in an array
while (i < len)
{
  optionsArray.push(options[i++].value);
}
    return optionsArray;
}

var t1 = document.getElementById("t1").innerHTML;
var t2 = document.getElementById("t2").innerHTML;
var t3 = document.getElementById("t3").innerHTML;
var bookedout = "sorry, booked out!"

// check if condition is met
if (t1 == "available (0 seats)"){
    doUpdateDOM("t1")
}
if (t2 == "available (0 seats)"){
doUpdateDOM("t2")
}
if (t3 == "available (0 seats)"){
doUpdateDOM("t3")
}

function doUpdateDOM(nodeID){
    document.getElementById(nodeID).innerHTML = bookedout;
    var textArr = document.getElementById(nodeID).parentElement.innerText.split(':');
 mySelect.remove(optionsArray.indexOf(textArr [0]))
}

Assuming that the order of the options inside the select correspond to the order inside the table element you could simply do something like this. 假设select中的选项顺序对应于table元素内的顺序,你可以简单地做这样的事情。

 var select = document.getElementById('selectbox'); var table = document.getElementsByClassName('status').item(0); var rows = table.rows; var bookedout = " sorry, booked out!"; // check whether an option is bookedout // and save its state to a new array. var bookedOutState = [].slice.call(rows).map(function(row) { var match = row.children[0].textContent.match(/\\d/); if (+match[0] === 0) { row.children[0].textContent = match['input'].substr(0, match['input'].indexOf(':') + 1) + bookedout; return false; } return true; }) // go over the new created array and remove // options from select according to the saved state. bookedOutState.forEach(function(state, idx) { if (!state) { select.removeChild(select.children[idx]) } }) 
 <select id="selectbox"> <option>Berlin</option> <option>Munich</option> <option>Cologne</option> </select> <table class="status"> <tr><td>Berlin: <span id="t1">available 0 seats</span></td></tr> <tr><td>Munich: <span id="t2">available 1 seats</span></td></tr> <tr><td>Cologne: <span id="t3">available 2 seats</span></td></tr> </table> 

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

相关问题 Javascript - 禁用基于所选选项的选择选项元素 - Javascript - Disable Select Option Elements Based on Selected Option 禁用选项选择JavaScript - Disable option select javascript JavaScript禁用选择选项 - JavaScript Disable Select Option 禁用/启用基于选择Javascript的无线电/选择选项的文本框 - Disable/Enable a text box based on radio/select option selected Javascript 根据其他选择选项禁用选择选项 - Disable select option based on another select option 如何基于时间范围禁用javascript中数据库填充的选择选项 - How to disable a select option populated by database in javascript based on time range 如何禁用 <option>在一个<select>基于它在 JavaScript 中的值?我有一个带有多个 &lt;option&gt; 的 &lt;select&gt; 。每一个都有独特的价值。我需要禁用具有给定定义值(而不是 innerHTML )的 &lt;option&gt; 。有人知道怎么做吗?</select></option> - How can I disable an <option> in a <select> based on its value in JavaScript? 如何基于另一个选择选项禁用选择选项 - How to disable select option based on another select option 如何禁用 <select>基于其他的选择<select>选择了选项? - How to disable <select> option based on other <select> option selected? 如何禁用基于另一个选择选项的选择选项? - How do I disable a select option based on another select option?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM