jQuery Mobile识别表单中的单击按钮并更改值

时间:2014-12-04 19:15:39

标签: jquery forms jquery-mobile submit-button

我在jQuery移动页面上有多个表单,它们都使用相同的php处理器。我能够想出如何让jQuery只提交包含用户点击的提交按钮的表单,但我无法弄清楚如何更改用户点击的按钮的值。

如果按钮文本只能更改用户单击的按钮,我希望如此。

到目前为止,下面的脚本将更改所有提交按钮的文本,而不仅仅是用户点击的按钮。

我的jQuery:

$("form input[type=submit]").on("click", function(e){
    $.post('doStuff.php', $(this).closest("form").serialize(), function(data) {
        //changes the value of all submit buttons    
        $('input[type=submit]').val('Thank You').button( "refresh" );       

        //the rest of these do not work at all      
        $('input[type=submit]', $(this).closest('form')).val('Thank You').button( "refresh" );
        $('input[type=submit]', $(this)).val('Thank You').button( "refresh" );
        $('input[type=submit]', $(this).parent('form')).val('Thank You').button( "refresh" );       
    }).error(function(){
        alert("Error submiting the form");
    });
});

我的HTML:

<form action="doStuff.php" method="post" data-ajax='false'>
   <input type="submit" name="submit" value="stuff" id="btnSubmit">
</form>
<form action="doStuff.php" method="post" data-ajax='false'>
   <input type="submit" name="submit" value="more stuff" id="btnSubmit">
</form>
<form action="doStuff.php" method="post" data-ajax='false'>
   <input type="submit" name="submit" value="a ton of stuff" id="btnSubmit">
</form>

有没有人知道如何完成我想要做的事情?

2 个答案:

答案 0 :(得分:1)

以其他方式执行,听取submit事件,发布表单并更改该表单中按钮的文本。

$("form").on("submit", function () {
  $(this).find("[type=submit]").val("Thank you").button("refresh");
});

答案 1 :(得分:0)

你的回答大多是正确的....谢谢你!在发布表单之前,我必须先将$(this)分配给变量。以下是工作代码。

$("form input[type=submit]").on("click", function(e){
    e.preventDefault();
    $this = $(this);
    $.post('doStuff.php', $(this).closest("form").serialize(), function(data) {
        $this.find("[type=submit]").val("Thank you").button("refresh");     
    }).error(function(){
        alert("Error submiting the form");
    });
}); 
相关问题