how to get checkbox value in javascript [closed]

放肆的年华 提交于 2019-12-25 18:54:28

问题


My HTML script is

<html>

    <body>
    <b><font color="green">Please select a check box to change the color of text </font> </b><br>
    <input type="checkbox" name="red"  id="chkbx" > &nbsp; Red<br>
    <input type="checkbox" name="green" id="chkbx" value="1"> &nbsp; Green <br>
    <input type="checkbox" name="blue" id="chkbx" value="2"> &nbsp; Blue <br>
    <input type="checkbox" name="orange" id="chkbx" value="3"> &nbsp; Orange <br>
    <input type="checkbox" name="yellow" id="chkbx" value="4"> &nbsp; Yellow <br>
    <p id="demo"></p>

    <button onclick="myfunction()">My Choice</button>
<br><br>
    <h style="color: red; font-weight: bold;"> </h>
    <p id="demo"></p>

i need to get the checkbox value in javascript and if any one checkbox is selected that colour will apply to a text to display on the output. how to do?


回答1:


First off - don't use the tag as it has been deprecated since 1999 use

<p style='font-weight:bold; color:green'>....</p>

Instead of checkboxs use radio buttons

<input type="radio" name="red" value="red" onClick="myFunction(this.value);"> &nbsp; Red<br>

Repeat the above for all possible selections. Change your function myFunction() to myFunction( value ) and work from there. The information is passed directly to the function so there is no need to go looking for it.



来源:https://stackoverflow.com/questions/21668843/how-to-get-checkbox-value-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!