简体   繁体   English

尝试输出表格列表中单选按钮的值

[英]Trying to output value of radio buttons in table list

so I'm trying to output the value from radio buttons into a table, but no matter what I pick it just says 0, and then goes away very quickly. 因此我试图将单选按钮的值输出到表中,但是无论我选择什么,它都只是说0,然后很快消失。 I want the results to stay up but kind of as a vertical list so it would look something like: 0 1 2 1, etc. 我希望结果保持不变,但有点像一个垂直列表,所以它看起来像是:0 1 2 1,等等。

 function display() { document.getElementById("displayarea").innerHTML = document.getElementById("dis1").value; document.getElementById("dis1").value = "0"; document.getElementById("displayarea1").innerHTML = document.getElementById("som1").value; document.getElementById("som1").value = "1"; document.getElementById("displayarea").innerHTML = document.getElementById("stron1").value; document.getElementById("stron1").value = "2"; } 

So this is just some of the script because it's very long, I'm sure there's an easier way but I'm new to JavaScript. 所以这只是一些脚本,因为它很长,我敢肯定有一种更简单的方法,但是我是JavaScript的新手。 Each radio button is either dis, som, or stron ranging from 1 to 6. 每个单选按钮可以是dis,som或stron,范围是1到6。

  <table width="400px" align="center" border=0> <tr style="background-color:#8FBC8F;"> <td align="center"><b>Results</b></td> </tr> <tr> <td align="center"><div id="displayarea"></div></td> </tr> </table> 

This is where it outputs to, like I said it keeps saying 0 very quickly and disappearing. 就像我说过的那样,它一直输出到0并很快消失。 Like it's reading all the values as 0. 就像正在读取所有值为0的值一样。

  <label>I have trouble falling asleep or sleeping too much</label> <fieldset class="options2"> <input type="radio" name="dis2" id="dis2" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som2" id="som2" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron2" id="stron2" value="2"/> <label>Strongly agree</label> </fieldset> 

And here's a sample of one of the button fieldsets. 这是一个按钮字段集的示例。 I appreciate any help. 感谢您的帮助。 My main focus is getting it to stop reading all the button fields as 0 and going away very quickly. 我的主要重点是让它停止将所有按钮字段读取为0并很快消失。

Sorry I left out that the buttons are in a form that runs when the submit button is clicked. 抱歉,我忽略了这些按钮处于单击提交按钮时运行的形式。 <input type="submit" id="submit" value="submit" onClick="display()"/></td>

Another edit: the entire form 另一个编辑:整个表格

<form id="dep" name="dep">

<fieldset id="depfield">    
<label>I've lost my enthusiasm for life</label>
    <fieldset class="options1">
        <input type="radio" name="dis1" id="dis1" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som1" id="som1" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron1" id="stron1" value="2"/>
        <label>Strongly agree</label>   
    </fieldset>

  <label>I have trouble falling asleep or sleeping too much</label>
    <fieldset class="options2">
        <input type="radio" name="dis2" id="dis2" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som2" id="som2" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron2" id="stron2" value="2"/>
        <label>Strongly agree</label>   
    </fieldset>

     <label>I have suicidal thoughts</label>
    <fieldset class="options3">
        <input type="radio" name="dis3" id="dis3" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som3" id="som3" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron3" id="stron3" value="2"/>
        <label>Strongly agree</label>   
    </fieldset>

     <label>I tend to over-eat or eat too little</label>
    <fieldset class="options4">
        <input type="radio" name="dis4" id="dis4" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som4" id="som4" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron4" id="stron4" value="2"/>
        <label>Strongly agree</label>   
    </fieldset>

     <label>I often feel very emotional and upset</label>
    <fieldset class="options5">
        <input type="radio" name="dis5" id="dis5" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som5" id="som5" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron5" id="stron5" value="2"/>
        <label>Strongly agree</label>   
    </fieldset>

    <label>I don't put in as much effort as I used to</label>
    <fieldset class="options6">
        <input type="radio" name="dis6" id="dis6" value="0"/>
        <label>Strongly disagree</label>
        <input type="radio" name="som6" id="som6" value="1"/>
        <label>Somewhat agree</label>
        <input type="radio" name="stron6" id="stron6" value="2"/>
        <label>Strongly agree</label>   
    </fieldset> 
</fieldset>
<p>
<input type="submit" id="submit" value="submit" onClick="display()"/></td>
</p>
</form>

For radio buttons, every element should have the same name.Otherwise it won't function properly 对于单选按钮,每个元素应具有相同的名称,否则将无法正常工作

Eg: 例如:

<fieldset class="options2">

    <input type="radio" name="nameofradiobutton" id="dis2" value="0"/>
    <label>Strongly disagree</label>
    <input type="radio" name="nameofradiobutton" id="som2" value="1"/>
    <label>Somewhat agree</label>
    <input type="radio" name="nameofradiobutton" id="stron2" value="2"/>
    <label>Strongly agree</label>   

</fieldset>

You do not need separate id's, simply make the name property same to act them as a group. 您不需要单独的ID,只需将name属性设为相同即可将它们作为一个组。 You can use querySelectorAll() to select all the radios, then use map() to get all the values. 您可以使用querySelectorAll()选择所有单选,然后使用map()获取所有值。 Finally join() them: 最后join()他们:

 function display(){ var el = [...document.querySelectorAll('input[type=radio]')]; el = el.map(r => r.value).join(' '); document.getElementById("displayarea").textContent = el; } 
 <table width="400px" align="center" border=0> <tr style="background-color:#8FBC8F;"> <td align="center"><b>Results</b></td> </tr> <tr> <td align="center"><div id="displayarea"></div></td> </tr> </table> <form id="dep" name="dep"> <fieldset id="depfield"> <label>I've lost my enthusiasm for life</label> <fieldset class="options1"> <input type="radio" name="dis1" id="dis1" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som1" id="som1" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron1" id="stron1" value="2"/> <label>Strongly agree</label> </fieldset> <label>I have trouble falling asleep or sleeping too much</label> <fieldset class="options2"> <input type="radio" name="dis2" id="dis2" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som2" id="som2" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron2" id="stron2" value="2"/> <label>Strongly agree</label> </fieldset> <label>I have suicidal thoughts</label> <fieldset class="options3"> <input type="radio" name="dis3" id="dis3" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som3" id="som3" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron3" id="stron3" value="2"/> <label>Strongly agree</label> </fieldset> <label>I tend to over-eat or eat too little</label> <fieldset class="options4"> <input type="radio" name="dis4" id="dis4" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som4" id="som4" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron4" id="stron4" value="2"/> <label>Strongly agree</label> </fieldset> <label>I often feel very emotional and upset</label> <fieldset class="options5"> <input type="radio" name="dis5" id="dis5" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som5" id="som5" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron5" id="stron5" value="2"/> <label>Strongly agree</label> </fieldset> <label>I don't put in as much effort as I used to</label> <fieldset class="options6"> <input type="radio" name="dis6" id="dis6" value="0"/> <label>Strongly disagree</label> <input type="radio" name="som6" id="som6" value="1"/> <label>Somewhat agree</label> <input type="radio" name="stron6" id="stron6" value="2"/> <label>Strongly agree</label> </fieldset> </fieldset> <p> <input type="button" id="submit" value="submit" onClick="display()"/> </p> </form> 

If you want to get the values on change of the radio try the following: 如果要获取无线电更改时的值,请尝试以下操作:

 document.querySelectorAll('input[name=asleep]').forEach(function(el){ el.addEventListener('change', display); }); function display(){ document.getElementById("displayarea").textContent = this.value; } 
 <table width="400px" align="center" border=0> <tr style="background-color:#8FBC8F;"> <td align="center"><b>Results</b></td> </tr> <tr> <td align="center"><div id="displayarea"></div></td> </tr> </table> <label>I have trouble falling asleep or sleeping too much</label> <fieldset class="options2"> <input type="radio" name="asleep" value="0"/> <label>Strongly disagree</label> <input type="radio" name="asleep" value="1"/> <label>Somewhat agree</label> <input type="radio" name="asleep" value="2"/> <label>Strongly agree</label> </fieldset> 

Your main problem was that your form was submitting and therefore the site got reloaded -> all values disappeared. 您的主要问题是您的form正在提交,因此网站被重新加载->所有值都消失了。

To fix this, you can either change type="submit" of your button to type="button" or use event.preventDefault() like I did in my example. 要解决此问题,您可以将按钮的type="submit"更改为type="button"或像在我的示例中一样使用event.preventDefault()

You also can shorten your script to something like this: 您还可以将脚本缩短为以下形式:

 function display(event) { // Need "event" to access prevenDefault(); event.preventDefault(); // This line prevents the form from submitting, so your values stay visible after clicking the button var radios = [...document.querySelectorAll('input[type=radio]')]; // Get all your radio buttons at once var displayarea = document.getElementById('displayarea'); var values = radios.map(radio => radio.value).join(' '); // Get all values of your radios and concat them in a string displayarea.innerText = values; } 
 <form id="dep" name="dep"> <fieldset id="depfield"> <label>I've lost my enthusiasm for life</label> <fieldset class="options1"> <input type="radio" name="dis1" id="dis1" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som1" id="som1" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron1" id="stron1" value="2" /> <label>Strongly agree</label> </fieldset> <label>I have trouble falling asleep or sleeping too much</label> <fieldset class="options2"> <input type="radio" name="dis2" id="dis2" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som2" id="som2" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron2" id="stron2" value="2" /> <label>Strongly agree</label> </fieldset> <label>I have suicidal thoughts</label> <fieldset class="options3"> <input type="radio" name="dis3" id="dis3" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som3" id="som3" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron3" id="stron3" value="2" /> <label>Strongly agree</label> </fieldset> <label>I tend to over-eat or eat too little</label> <fieldset class="options4"> <input type="radio" name="dis4" id="dis4" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som4" id="som4" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron4" id="stron4" value="2" /> <label>Strongly agree</label> </fieldset> <label>I often feel very emotional and upset</label> <fieldset class="options5"> <input type="radio" name="dis5" id="dis5" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som5" id="som5" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron5" id="stron5" value="2" /> <label>Strongly agree</label> </fieldset> <label>I don't put in as much effort as I used to</label> <fieldset class="options6"> <input type="radio" name="dis6" id="dis6" value="0" /> <label>Strongly disagree</label> <input type="radio" name="som6" id="som6" value="1" /> <label>Somewhat agree</label> <input type="radio" name="stron6" id="stron6" value="2" /> <label>Strongly agree</label> </fieldset> </fieldset> <p> <input type="submit" id="submit" value="submit" onClick="display(event)" /></td> <!-- Need "event" to access preventDefault();" --> </p> </form> <table width="400px" align="center" border=0> <tr style="background-color:#8FBC8F;"> <td align="center"><b>Results</b></td> </tr> <tr> <td align="center"> <div id="displayarea"></div> </td> </tr> </table> 

add an onchange event to the radio buttons 向单选按钮添加一个onchange事件

  <label>I have trouble falling asleep or sleeping too much</label> <fieldset class="options2"> <input type="radio" name="r" value="0" onchange="showVal(this)"/> <label>Strongly disagree</label> <input type="radio" name="r" value="1" onchange="showVal(this)"/> <label>Somewhat agree</label> <input type="radio" name="r" value="2" onchange="showVal(this)"/> <label>Strongly agree</label> </fieldset> 

And then write a javascript function to display or do whatever 然后编写一个javascript函数来显示或执行任何操作

  function showVal(radio) { // possible things you may do, below are examples alert(radio.value); alert(radio.checked); }; 

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

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