简体   繁体   中英

How to set radio button checked to true on comparing it with textbox value?

I am trying to set the radio checked out of two radio buttons using JavaScript on the basis of the textbox value.

The textbox can only be filled with the values of radio buttons only else both the radios will be checked false because the values will be coming from database.

 function selradio() { if (document.getElementById("tvt").value == '6 Wheeler') { document.getElementById('6wt').checked = true; document.getElementById('10wt').checked = false; } else if (document.getElementById("tvt").value == '10 Wheeler') { document.getElementById('6wt').checked = false; document.getElementById('10wt').checked = true; } }
 <div class="col-sm-2"> <label> <input type="radio" name="r3" value="6 Wheeler" class="flat-red"> 6 Wheeler Truck </label> </div> <div class="col-sm-2"> <label> <input type="radio" name="r3" value="10 Wheeler" class="flat-red"> 10 Wheeler Truck </label> </div> <input type="text" id="tvt" name="tvt" onChange="selradio()">

    <div class="col-sm-2">
  <label>
    <input type="radio" id="6wt" name="r3" value="6 Wheeler" class="flat-red"> 
    6 Wheeler Truck
  </label>
</div>
<div class="col-sm-2">
  <label>
    <input type="radio" id="10wt" name="r3" value="10 Wheeler" class="flat-red"> 
    10 Wheeler Truck
  </label>
</div>

<input type="text" id="tvt" name="tvt" onChange="selradio()">

Please set the ID of radio button and it will work.

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