如何使用带有多个提交按钮的JQuery提交表单?

时间:2017-06-18 12:58:03

标签: javascript jquery html forms

我正在尝试制作一个有两个提交按钮的表单。这是表格。

<form method="POST" id="addCreditsForm">
    <div class="row">
        <div class="col s12 m10 offset-m1 l10 offset-l1 input-field">
            <input type="number" name="amount" id="amount" required/>
            <label for="amount">Amount</label>
        </div>
        <div class="col s12">
            <p id="addWithPaypal" style="margin-top:5px;margin-bottom:5px;" class="btn blue">Pay with Paypal</p>
            <p id="addWithInstamojo" style="margin-top:5px;margin-bottom:5px;" class="btn blue">Pay with Instamojo</p>
        </div>
    </div>
</form>

我正在尝试使用 JQuery 执行此操作。这是JQuery代码。

$(document).ready(function(){
    $("#addWithPaypal").click(function(){
        $("#addCreditsForm").attr('action'."paypal/add/credits");
        $("#addCreditsForm").submit();
    });

    $("#addWithInstamojo").click(function(){
        $("#addCreditsForm").attr('action'."instamojo/add/credits");
        $("#addCreditsForm").submit();
    });
});

但是当我点击负责在Event上调用JQuery函数的任何元素时,JQuery无法正常工作。我试图在两个按钮的点击事件上发出警报,但它也不起作用!

我不知道出了什么问题。请帮帮我!

1 个答案:

答案 0 :(得分:1)

错误而不是,你将.保留在

`$("#addCreditsForm").attr('action',"instamojo/add/credits");`
                                       ^here  

$("#addCreditsForm").attr('action',"paypal/add/credits");

&#13;
&#13;
$(document).ready(function(){
    $("#addWithPaypal").click(function(){
        $("#addCreditsForm").attr('action',"paypal/add/credits");
        $("#addCreditsForm").submit();
    });

    $("#addWithInstamojo").click(function(){
        $("#addCreditsForm").attr('action',"instamojo/add/credits");
        $("#addCreditsForm").submit();
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" id="addCreditsForm">
    <div class="row">
        <div class="col s12 m10 offset-m1 l10 offset-l1 input-field">
            <input type="number" name="amount" id="amount" required/>
            <label for="amount">Amount</label>
        </div>
        <div class="col s12">
            <p id="addWithPaypal" style="margin-top:5px;margin-bottom:5px;" class="btn blue">Pay with Paypal</p>
            <p id="addWithInstamojo" style="margin-top:5px;margin-bottom:5px;" class="btn blue">Pay with Instamojo</p>
        </div>
    </div>
</form>
&#13;
&#13;
&#13;