用于禁用或启用单选按钮的Javascript

时间:2013-12-11 04:58:31

标签: javascript html forms

我正在尝试做一些比这更复杂的事情,但我将其分解并将其扔进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 个答案:

答案 0 :(得分:2)

像这样发送函数调用者的详细信息

<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>