javascript 如何在javascript中显示选中的复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16425257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display the selected check box value in javascript
提问by mas
I want to dispaly the selected check box value if i click on the print button
如果单击打印按钮,我想显示选定的复选框值
here is my code
这是我的代码
<! DOCTYPE html>
<head>
<script type="text/javascript">
function checkbox()
{
var bk=document.getElementById("bk").value;;
var cer=document.getElementById("cr").value;
document.writeln(" " + bk );
document.write(" " + cer);
}
</script>
</head>
<body>
<form>
<input type="checkbox" id="bk" name="vehicle" value="Bike">I have a bike<br></br>
<input type="checkbox" id="cr" name="vehicle" value="Car">I have a car<br></br>
<input type="submit" value=" Print " size="30" onClick="checkbox()">
</form>
</body>
</html>
If I select Bike check box and click on print button, it should display the only Bike.
如果我选择自行车复选框并单击打印按钮,它应该显示唯一的自行车。
回答by Eon
i have edited your code and modified according to your needs. please go through the code.
我已经编辑了您的代码并根据您的需要进行了修改。请通过代码。
<! DOCTYPE html>
<head>
<script type="text/javascript">
function checkbox()
{
var bk=document.getElementById("bk").checked;;
var cer=document.getElementById("cr").checked;
if(bk) document.writeln(" Bike");
if(cer) document.write(" Car");
return false;
}
</script>
</head>
<body>
<form>
<input type="checkbox" id="bk" name="vehicle" value="Bike">I have a bike<br></br>
<input type="checkbox" id="cr" name="vehicle" value="Car">I have a car<br></br>
<input type="submit" value=" Print " size="30" onClick="return checkbox();">
</form>
</body>
</html>
回答by anthony
heres ur code.. this should solve ur problem..
继承人你的代码..这应该可以解决你的问题..
<! DOCTYPE html>
<head>
<script type="text/javascript">
function checkbox()
{
var bk=document.getElementById("bk").checked;;
var cer=document.getElementById("cr").checked;
if(bk) document.writeln(" Bike");
if(cer) document.write(" Car");
return false;
}
</script>
</head>
<body>
<form>
<input type="checkbox" id="bk" name="vehicle" value="Bike">I have a bike<br></br>
<input type="checkbox" id="cr" name="vehicle" value="Car">I have a car<br></br>
<input type="submit" value=" Print " size="30" onClick="return checkbox();">
</form>
</body>
</html>
回答by Ja?ck
This function would create an array with the values of each checked item inside your form:
此函数将创建一个数组,其中包含表单中每个选中项的值:
function checkbox()
{
var checked = [].reduce.call(document.forms[0].vehicle, function(current, item) {
if (item.checked) {
current.push(item.value);
}
return current;
}, []);
alert(checked);
}
Here, document.forms[0].vehicle
returns both checkboxes with the name vehicle
inside your form; I'm using document.forms[0]
because you didn't give the form a name :)
在这里,document.forms[0].vehicle
返回带有vehicle
表单中名称的两个复选框;我正在使用,document.forms[0]
因为您没有为表单命名:)