本文介绍了根据提交按钮更改表单操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有很多类似以下的表格.现在,我想根据用户单击的提交按钮(当然要提交表单)来更改表单操作

I have many forms like the following on the page. Now I want change the form action based on which submit button the user clicked (and of course submit the form)

<form action="/shop/products.php" data-ajax="false" method="post">
    <div data-role="fieldcontain">

        <input name="submit" class="obutn" type="submit" value="Order" />
        <input name="submit" class="oEbutn" type="submit" value="Extras" />

    </div>
</form>

我尝试过

$(".obtn").click(function() {
    $(this).parent().parent().attr("action", "/shop/products.php");
});
$(".oEbtn").click(function() {
    $(this).parent().parent().attr("action", "/shop/extras.php");
});

,但表格始终提交给products.php.你能告诉我怎么了吗?

but the form is always submited to products.php. Can you tell me what's wrong?

推荐答案

有两种错别字:

  • obtn而不是obutn
  • oEbtn而不是oEbutn
  • obtn instead of obutn
  • oEbtn instead of oEbutn

另一个建议是使用closest("form")来获取包含单击按钮的表单.

Another suggestion is to use closest("form") to get the form that contains the clicked button.

$(".obutn").click(function() {
    $(this).closest("form").attr("action", "/shop/products.php");
});
$(".oEbutn").click(function() {
    $(this).closest("form").attr("action", "/shop/extras.php");
});

$("form").on("submit", function () {
    alert($(this).attr("action"));
});

JSFIDDLE

这篇关于根据提交按钮更改表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:26