禁用或启用单选按钮的Javascript

Javascript to disable or enable radio buttons

本文关键字:Javascript 单选按钮 启用      更新时间:2023-09-26

我正试图做一些比这更复杂的事情,但我把它分解并扔到了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; 
     } 
}

发送函数调用程序的详细信息,如

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