Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过原型/构造函数从复选框输出值_Javascript - Fatal编程技术网

Javascript 通过原型/构造函数从复选框输出值

Javascript 通过原型/构造函数从复选框输出值,javascript,Javascript,我试图从表单元素输出值,我设法从选择元素输出单个值,但是如果选择了多个复选框,我如何输出多个值。看看有没有人能帮忙?也许我需要通过数组传递复选框值 <head><title></title></head> <style> #box{ border:solid 1px red; height:16px; } </style> <body> size : <select id = "test1"> &l

我试图从表单元素输出值,我设法从选择元素输出单个值,但是如果选择了多个复选框,我如何输出多个值。看看有没有人能帮忙?也许我需要通过数组传递复选框值

<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>

#盒子{
边框:实心1px红色;
高度:16px;
}
尺寸:
大的
中等的
小的
基数:
厚的
薄的
番茄:
洋葱:
辣椒粉:


功能比萨饼(s,t){ 这个尺寸=s; this.type=t; } Pizza.prototype.myPizza=函数(){ document.getElementById('box').innerHTML=“这是一个以“+This.type+”为底的“+This.size+”比萨饼,配料包括:”; } 函数按钮单击(){ x=document.getElementById('test1')。值; y=document.getElementById('test2')。值; 星期二=新比萨饼(x,y); 星期二,我的披萨(); }

代码也可以在此处查看:

这应该可以做到:

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();
}​
功能比萨饼(s、t、tops){
这个尺寸=s;
this.type=t;
这个。浇头=顶部;
}
Pizza.prototype.myPizza=函数(){
document.getElementById('box').innerHTML=“这是一个以“+This.type+”为底的“+This.size+”比萨饼,配料包括:“+This.toppings.join(',”);
};
函数按钮单击(){
x=document.getElementById('test1')。值;
y=document.getElementById('test2')。值;
z=新数组();
var chk_arr=document.getElementsByName(“topping[]);
对于(变量i=0;i
Hello-是否需要为复选框指定名称属性?例如番茄:洋葱:辣椒:如果你想把它们传递给服务器,让它被PHP这样的语言读取,那么是的。您好-我已经按照自己的建议添加了代码,但仍然无法在输出中看到浇头,我在这里添加了更新的代码:啊,好的-刚才看到了JSFIDLE示例,我以前没有注意到。是的,它正在工作。非常感谢:)