1

在以下示例中,当您选择选项id="chocolate-yes"和/或id="vanilla-yes"时,该选项id="icecreamcone-yes"也将被选中:

<ul id="icecream" style="list-style:none;line-height:30px;">
  <li>
    <select id="icecreamcone">
      <option value="addicecreamcone">Would you like an ice cream cone?</option>
      <option id="icecreamcone-yes" value="yes">Yes</option>
      <option id="icecreamcone-no" value="no">No thanks</option>
    </select>
  </li>
  <li>
    <select id="vanilla">
      <option value="addvanilla">Would you like to add vanilla ice cream?</option>
      <option id="vanilla-yes" value="yes">Yes</option>
      <option id="vanilla-no" value="no">No thanks</option>
    </select>
  </li>
  <li>
    <select id="chocolate">
      <option value="addchocolate">Would you like to add chocolate ice cream?</option>
      <option id="chocolate-yes" value="yes">Yes</option>
      <option id="chocolate-no" value="no">No thanks</option>
    </select>
  </li>
  <li>
    <select id="sprinkles">
      <option value="addsprinkles">Would you like to add sprinkles on top?</option>
      <option id="sprinkles-yes" value="yes">Yes</option>
      <option id="sprinkles-no" value="no">No thanks</option>
    </select>
  </li>
</ul>


<script type="text/javascript">
function observeFlavor(flavor, requires) {
    document.getElementById(flavor).onchange = function(){
      if(document.getElementById(flavor).options[1].selected == true) {
        document.getElementById(requires).options[1].selected = true
      }
    }
}

observeFlavor("chocolate","icecreamcone");
observeFlavor("vanilla","icecreamcone");
</script>


结合上面的脚本,有没有人知道如何在选择 , 或两者时使不可<select id="icecreamcone">点击,但是当它们都被取消选择时,将再次可点击?id="vanilla-yes"id="chocolate-yes"<select id="icecreamcone">


注意:不使用 disabled 属性。

4

2 回答 2

3

相信应该这样做:

<script type="text/javascript">
function observeFlavor(flavor, requires) {
    document.getElementById(flavor).onchange = function(){
      if(document.getElementById(flavor).options[1].selected == true) {
        document.getElementById(requires).options[1].selected = true;
        document.getElementById(requires).disabled = true;
      }
      else{           
        document.getElementById(requires).disabled = false;
      }
    }
}

document.getElementById("your-submit-button").onclick = function () {    
    document.getElementById("icecreamcone").disabled = false;
}

observeFlavor("chocolate","icecreamcone");
observeFlavor("vanilla","icecreamcone");
</script>
于 2012-11-15T10:49:40.777 回答
0

您可以利用Select Disabled 属性

于 2012-11-15T10:36:30.297 回答