提交多个表单提交

时间:2012-04-12 22:56:43

标签: javascript jquery forms html5

我一直在努力思考这一点并弄清楚它是否可能。我使用zen-cart作为购物车软件,但我想做的是,硬编码页面基本上是7-9个产品的列表,每个产品旁边都是一个复选框,所以我想要找出一种方法,通过html,javascript或jquery提交检查到购物车的任何形式(产品)。产品的典型表单提交看起来像这样(有时可能有一个或两个额外的隐藏字段):

<form name="cart_quantity" action="index.php?action=add_product" method="post"      enctype="multipart/form-data">
    <input type="hidden" name="cart_quantity" value="1">
    <input type="hidden" name="products_id" value="7">
    <input type="hidden" name="id[6]" value="9" id="attrib-6-9"> 
    <input type="image" src="buy_button.png" alt="Add to Cart" title="Instructional Video Part 1: Add to Cart">
</form>

页面上会有7-9个这些,每个都有一个复选框,所以我假设一个脚本需要找出哪些选中,并通过表单操作提交它们?也许有一种更好的方法可以解决这个问题,我没想到因为a)它超出了我的头脑或b)还没有想出来。无论如何这样的事情可能吗?

3 个答案:

答案 0 :(得分:0)

为什么不将它们全部放在同一个表格中?你有没有具体的理由为每个人分别形成一个表格?

将它们全部放在同一个表单中,并在提交页面(逻辑所在的位置)为所有复选框运行一个循环,并且只检查那些复选框,将它们添加到购物车中

复选框可以将值作为相应的产品ID ....

或类似的东西 在html页面中它可以有

Product1 <input typ='checkbox' name='checkbox[1]'>
Product2 <input typ='checkbox' name='checkbox[2]'>
Product3 <input typ='checkbox' name='checkbox[3]'>

在逻辑页面

foreach($_POST[checkbox] as $k=>$v){
    if($v=='checked')
         //add to cart  like $_POST[product[$k]]  $_POST[amount[$k]] if there are such elements on the html page
}

答案 1 :(得分:0)

你可以这样做,

$("#yourcommonbuton").click(function() {
    if($("#checkbox1").prop('checked') == true) $("#form1").submit();
    if($("#checkbox2").prop('checked') == true) $("#form2").submit();
    if($("#checkbox3").prop('checked') == true) $("#form3").submit();
    return false;
});

答案 2 :(得分:0)

为每个表单添加一些复选框,其中包含一个类:

<form name="cart_quantity" action="index.php?action=add_product" method="post"      enctype="multipart/form-data">
    <input type="checkbox" class="IsCheck" />
    <input type="hidden" name="cart_quantity" value="1">
    <input type="hidden" name="products_id" value="7">
    <input type="hidden" name="id[6]" value="9" id="attrib-6-9"> 
    <input type="image" src="buy_button.png" alt="Add to Cart" title="Instructional Video Part 1: Add to Cart">
</form>

在所有表单下方添加一个按钮,根本不必在表单内:

<input type="button" value="Submit Selected" id="submit_btn" />

jQuery的:

$('#submit_btn').on('click', function() {
    $('.IsCheck').is(':checked').each(function() { 
        $(this.form).submit(); 
    });
});