onclick事件不会调用我的函数

时间:2014-03-15 20:34:20

标签: javascript html5 function onclick

所以我试着制作一个简单的披萨订购页面,但从我所知道的,我的代码没有任何问题。但是,当用户点击下订单按钮时​​,我创建的javascript函数不会被调用!我不确定我做错了什么!

我已经发布了我的代码,在这里,任何帮助表示赞赏!谢谢!

这里是函数placeOrder(f)

function finalOrder(f)
        {
            var response;
            var osize;
            var otop = "";
            var ocrust;
                if (f.psize[0].checked == true)
                      osize = f.psize[0].value;
                  else if (f.psize[1].checked == true)
                           osize = f.psize[1].value;
                  else if (f.psize[2].checked == true)
                           osize = f.psize[2].value;
                  else if (f.psize[3].checked == true)
                           osize = f.psize[3].value;

                  if (f.crust[0].checked == true)
                      ocrust = f.crust[0].value;
                  else if (f.crust[1].checked == true)
                           ocrust = f.crust[1].value;
                  else if (f.crust[2].checked == true)
                           ocrust = f.crust[2].value;
                  else if (f.crust[3].checked == true)
                           ocrust = f.crust[3].value;

                  if (f.cb1.checked == true)
                      otop = otop + f.cb1.value;
                  if (f.cb2.checked == true)
                      otop = otop + f.cb2.value;
                  if (f.cb3.checked == true)
                      otop = otop + f.cb3.value;
                  if (f.cb4.checked == true)
                      otop = otop + f.cb4.value;
                  if (f.cb5.checked == true)
                      otop = otop + f.cb5.value;
                  if (f.cb6.checked == true)
                      otop = otop + f.cb6.value;
                  if (f.cb7.checked == true)
                      otop = otop + f.cb7.value;
                  if (f.cb8.checked == true)
                      otop = otop + f.cb8.value;
                  if (f.cb9.checked == true)
                      otop = otop + f.cb9.value;
                  if (f.cb1o.checked == true)
                    otop = otop + f.cb10.value;

                  response = confirm("Please confirm: a "+osize+" with "+otop+""+ocrust+" from the store in "+document.orderDetails.storeMenu.value+" for a price of "+document.orderDetails.displayTotal.value);
                   if (response==true)
                    {
                            alert("Thank you for your order!");
                       }
                    else
                        {
                            alert("Your order has been cancelled.");
                        }
        }

这是我使用onclick事件调用它的地方

<p>
            Price Including Tax:
                <input  id="tax" type="text" name="displayTotal" size="10">
                <input  id="placeOrder" type="button" name="placeOrder" value="Place Order" onclick = "finalOrder(document.pizzaSize);">
        </p>

pizzaSize是用户为披萨选择项目的另一种形式,我已经确认它是正确的。

任何帮助都很棒,谢谢!

更新: 这是我做的小提琴的链接 - http://jsfiddle.net/jcritch/L8A4a/

1 个答案:

答案 0 :(得分:0)

如果检查蝙蝠侠,你应该真的尝试简化这个。

function finalOrder(f) {
    var response,
        osize,
        otop = "",
        ocrust;

    for(var i = f.psize.length; i--;){
        if(f.psize[i].checked)
            osize = f.psize[i].value;
            break;
        }
    }

    for(var i = f.crust.length; i--;){
        if (f.crust[i].checked)
            ocrust = f.crust[i].value;
            break;
        }
    }

    for(var i = 0; i < 10; i++){
        var checkbox = f.getElementsByName('cb'+(i+1))[0];

        if (checkbox.checked)
            otop += checkbox.value;
        }
    }

    var response = confirm("Please confirm: a "+osize+" with "+otop+""+ocrust+" from the store in "+document.orderDetails.storeMenu.value+" for a price of "+document.orderDetails.displayTotal.value);

    if (response) {
        alert("Thank you for your order!");
    } else {
        alert("Your order has been cancelled.");
        return false;
    }
}

我还注意到您正在检查reply而不是response,这是您的变量。可能这就是为什么你没有看到消息。我添加了return false,因为我认为如果取消,我不想处理该表单。