通过原型/构造函数从复选框输出值
Output values from checkboxes via protoype/constructor
我试图从表单元素输出值,我设法从select元素输出单个值,但如果选择了多个复选框,我如何输出多个值。看看有没有人能帮忙?也许我需要通过数组传递复选框值!
<head><title></title></head>
<style>
#box{
border:solid 1px red;
height:16px;
}
</style>
<body>
size : <select id = "test1">
<option>Large</option>
<option>Medium</option>
<option>small</option>
</select>
Base : <select id = "test2">
<option>Thick</option>
<option>Thin</option>
</select>
Tomato:<Input type ="checkbox">
Onion:<Input type ="checkbox">
Paprika:<Input type ="checkbox">
<input type="submit" value = "Submit" onclick ="buttonClick()" />
<br /> <br />
<div id ="box"></div>
<script type = "text/javascript">
function Pizza(s,t){
this.size = s;
this.type = t;
}
Pizza.prototype.myPizza = function(){
document.getElementById('box').innerHTML = "This is a " + this.size + " Pizza with " + this.type + " base and the toppings include: ";
}
function buttonClick(){
x = document.getElementById('test1').value;
y = document.getElementById('test2').value;
Tuesday = new Pizza(x,y);
Tuesday.myPizza();
}
</script>
</body>
</html>
代码也可以在此处查看:http://jsfiddle.net/bhEeZ/2/
这应该做到:jsFiddle示例
function Pizza(s, t, tops) {
this.size = s;
this.type = t;
this.toppings = tops;
}
Pizza.prototype.myPizza = function() {
document.getElementById('box').innerHTML = "This is a " + this.size + " Pizza with " + this.type + " base and the toppings include: " + this.toppings.join(', ');
};
function buttonClick() {
x = document.getElementById('test1').value;
y = document.getElementById('test2').value;
z = new Array();
var chk_arr = document.getElementsByName("topping[]");
for (var i = 0; i < chk_arr.length; i++) {
if (document.getElementsByName("topping[]")[i].checked) z.push(document.getElementsByName("topping[]")[i].value);
}
Tuesday = new Pizza(x, y, z);
Tuesday.myPizza();
}
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- 为复选框javascript指定两个值
- 如何在单击复选框后调用控制器方法
- 从复选框和Selects-KnockoutJS中获取值的总和
- 选中多个具有相同名称的复选框
- 使用Jquery选择或取消选择ListView中的所有复选框
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 通过原型/构造函数从复选框输出值
- 如何添加单选按钮和复选框将选择输入到一起以形成JavaScript输出
- 流星 - 如何使用流星输出复选框值.js
- 如何在使用值时输出复选框详细信息
- jQuery:将复选框的视觉输出更改为checked
- 单击按钮并根据复选框b中的设置值输出消息和打开新页面
- JQuery-输出选中复选框的类名
- 用javascript构建带有输入复选框的html输出
- JavaScript -根据选中的复选框更改输出值
- 我正在尝试将变量分配给复选框,并在选中时将该变量添加到输出变量中
- Javascript:根据按下的复选框显示输出
- Javascript/JQuery遍历表行和单元格,并向控制台输出一组选中的复选框