JavaScript禁用或启用单选按钮

4

我试图做一些比这更复杂的事情,但是我把它分解并放入JSFIDDLE中,仍然无法使其工作。

有什么建议吗?

JSFiddle

HTML:

<label for="service-type">Value</label>
<select id="service-type" onChange="hamburger()">
  <option>0</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>
<label for="receiptNo">No</label>
<input type="radio" name="receipt" id="receiptNo">
<label for="receiptYes">Yes</label>
<input type="radio" name="receipt" id="receiptYes">

Javascript:

var e = document.getElementById("service-type");
var ServiceUser = e.options[e.selectedIndex].text;

function hamburger() {
   if (ServiceUser.value === "2") {
      document.getElementById("receiptNo").disabled = true;
      document.getElementById("receiptYes").checked = true; 
     } 
}
1个回答

3
像这样发送函数调用者的详细信息
<select id="service-type" onchange="hamburger(this)">
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<input type="radio" name="receipt" id="receiptNo">
<input type="radio" name="receipt" id="receiptYes">

用JavaScript完成它

<script type="text/javascript">
    function hamburger(sender) { // sender here has all details of dropdown
        if (sender.value === "2") {
            document.getElementById("receiptNo").disabled = true;
            document.getElementById("receiptYes").checked = true;
        }
    }
</script>

就是这样!我知道我忘记了什么。我已经太久没有写函数了。 - CorporalAris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接