如何根据在前一个“select”元素上选择的内容,对要锁定的“select”元素上显示的某些值进行编码

问题描述 投票:0回答:2

是否可以根据在先前“select”元素上选择的内容来编码“select”元素上显示的某些值。防爆。如果客户在“选择”元素行1上选择了家庭计划主线(在下面的图像链接中突出显示)(例如下面图像链接中第一个设备的蜂窝计划),那么在选择其他“选择”元素行的计划时(例如,下面的图像链接中的第二个设备的蜂窝计划)不再能够选择家庭计划 - 主线,但所有其他计划都可以选择,例如家庭计划 - 附加线。 我似乎无法为此找到一个可靠的解决方案,但我不是最好的js,所以希望你们其中一个可以指出我正确的方向。

谢谢 :)

编辑:这是我正在使用的成本计算器:http://documentation.bold-themes.com/cost-calculator/whats-in-the-box/

javascript
2个回答
0
投票

是 - 使用disabled属性:

document.getElementById("one").addEventListener("change", e => {
  document.getElementById("two").querySelectorAll("option").forEach(o => {
    if ((e.target.value == "even" && parseInt(o.value) % 2 != 0) || (e.target.value == "odd" && parseInt(o.value) % 2 == 0)) {
      o.disabled = "disabled";
    } else {
      o.disabled = "";
    }
  });
});
<select id="one">
  <option value="even">Even numbers</option>
  <option value="odd">Odd numbers</option>
</select>
<select id="two">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
</select>

0
投票

如果您正在使用普通的旧javascript,那么您可以执行以下操作:

const select1 = document.getElementById('select1');
const select2 = document.getElementById('select2');


select1.addEventListener('change', function(event) {
    if(event.target.value === "1") {
        select2.querySelector('option[value="1"]').disabled = true;
        select2.querySelector('option[value="2"]').disabled = false;
        select2.querySelector('option[value="3"]').disabled = true;
    }
    else {
        select2.querySelector('option[value="1"]').disabled = false;
        select2.querySelector('option[value="2"]').disabled = true;
        select2.querySelector('option[value="3"]').disabled = false;
    }
});
<select id="select1">
<option value="1">Disable 1 and 3</option>
<option value="2">Disable 2</option>
</select>

<select id="select2">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
© www.soinside.com 2019 - 2024. All rights reserved.