如何在javascript中获取复选框值 [英] how to get checkbox value in javascript
本文介绍了如何在javascript中获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的HTML脚本是
<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" > Red<br>
<input type="checkbox" name="green" id="chkbx" value="1"> Green <br>
<input type="checkbox" name="blue" id="chkbx" value="2"> Blue <br>
<input type="checkbox" name="orange" id="chkbx" value="3"> Orange <br>
<input type="checkbox" name="yellow" id="chkbx" value="4"> 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>
我需要获取javascript中的复选框值,如果选择任何一个复选框,要在输出上显示的文本。如何做?
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?
推荐答案
首先 - 不要使用标记,因为它自1999年以来已被弃用
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);"> Red<br>
对所有可能的选择重复上述步骤。
将您的函数 myFunction()
更改为 myFunction(value)
信息直接传递给函数,因此不需要去寻找它。
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.
这篇关于如何在javascript中获取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文