简体   繁体   中英

Javascript to disable or enable radio buttons

I'm trying to do something a bit more complicated than this, but I broke it down and threw it in JSFIDDLE and I still can't get it to work.

Any suggestions?

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

Send the details of function caller like this

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

Finish it up with 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM