简体   繁体   English

Javascript根据文本框值检查表单中的单选按钮

[英]Javascript check a radio button in form based on a text box value

I have a form which inserts and retrieves data from a google sheet. 我有一个表单,可以从Google工作表中插入和检索数据。

Example: 例:

I have two radio buttons on my form 我的表格上有两个单选按钮

<input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" 
    value="1" onchange="RadioValInsert ()"/>

<input id="Rdio_2" name="RdioSelect"  type="radio" class="FirstCheck" 
    value="2" onchange="RadioValInsert ()" />

when the above is clicked the value of the radio button is stored in a text box..the RadioValInsert () does it 单击上面的按钮时,单选按钮的值将存储在文本框中RadioValInsert ()执行此操作

<input type="text" id="DatafromRadio" name="DatafromRadio">

I am able to insert this value of 1 or 2 into the corresponding cell in google sheet. 我可以将此值1或2插入Google表格中相应的单元格中。

When I want to EDIT it, I retrieve the data and the Textbox value is 1 or 2 当我想要编辑它时,我检索数据并且“文本框”值为1或2

The button which retrieves the data has a function to check the corresponding radio button based on the value of the Text box. 检索数据的按钮具有根据“文本”框的值检查相应单选按钮的功能。

function RadioChk() {
  var val = document.getElementById("DatafromRadio").value;
  if (val == 1) {
    document.getElementById("Rdio_1").checked = true;
  }
  if (val == 2) {
    document.getElementById("Rdio_2").checked = true;
  }
}

This is not working 这不行

Thanks in advance for your help 在此先感谢您的帮助

You are doing your check and uncheck related code inside RadioChk function however you haven't bind click event on radio inputs . 您正在RadioChk函数中checkuncheck check相关代码,但是尚未在单选输入上绑定click event If i correctly understood your question , here is how you can select and deselect your radio buttons. 如果我正确理解了您的问题,请按以下步骤选择和取消选择单选按钮。

 function RadioValInsert() { console.log('checked'); var val = document.getElementById("DatafromRadio").value; if(val ==1 || val ==2){ uncheckAll(); document.getElementById("Rdio_"+val).checked = true; }else{ uncheckAll(); console.log('choose only between 1 or 2'); } } function uncheckAll(){ let ele = document.getElementsByName("RdioSelect"); for(var i=0;i<ele.length;i++){ ele[i].checked = false; } } 
 <input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" value="1" onchange="RadioValInsert ()"/> <input id="Rdio_2" name="RdioSelect" type="radio" class="FirstCheck" value="2" onchange="RadioValInsert ()" /> <input type="text" id="DatafromRadio" name="DatafromRadio"> 

Try this example where radio selection and textbox value changes as per selection/input 尝试以下示例,其中单选和文本框值根据选择/输入而变化

 document.addEventListener('DOMContentLoaded', function(dce) { var radios = document.querySelectorAll('[name="RdioSelect"]'); var textbx = document.querySelector('[name="DatafromRadio"]'); radios.forEach(function(r) { r.addEventListener('click', function(e) { textbx.value = this.value; }); }); textbx.addEventListener('input', function(e) { radios.forEach(function(r) { r.checked = (r.value === textbx.value); }); }); var fillTxt = function(txt) { textbx.value = txt; textbx.dispatchEvent(new Event('input')); //<-- trigger event }; fillTxt('2'); //<--- update text box }); 
 <input id="Rdio_1" name="RdioSelect" type="radio" value="1" /> <input id="Rdio_2" name="RdioSelect" type="radio" value="2" /> <input type="text" id="DatafromRadio" name="DatafromRadio" /> 

this works only if data is input physically in the text box - in my case the data is retrieved via a function and it populates the text box 仅当数据实际输入到文本框中时,此方法才有效-在我的情况下,数据是通过函数检索的,并填充了文本框

In that, just trigger event on textbox 在那种情况下,只需在文本框上触发事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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