在输入类型=提交以外的元素上提交表单

时间:2013-06-28 21:24:44

标签: form-submit

我想通过单击提交按钮以外的元素来提交表单,在本例中为

。我以为我可以这样做,但表单不会在点击事件上提交,但会在我点击提交按钮时提交。

HTML     

提交

                                     

JQ     $(document).ready(function(){

$('p').click(function() {
$('form').submit(function(data) {  
    alert($(this).serialize()); // check to show that all form data is being submitted
    $.post("serializearray.php",$(this).serialize(),function(data){
        alert(data); //post check to show that the mysql string is the same as submit                        
    });
    return false; // return false to stop the page submitting. 
});
});

})

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

$(document).ready(function() {

  $('form').submit(function(data) {  
    alert($(this).serialize()); // check to show that all form data is being submitted
    $.post("serializearray.php",$(this).serialize(),function(data){
        alert(data); //post check to show that the mysql string is the same as submit                        
    });
    return false; // return false to stop the page submitting. 
  });

  $('p').click(function() {
    $('form').submit();
  });

});

当您将函数传递给$('form').submit(function(){})方法时,您绑定了一个回调,因此它不会执行。所以绑定回调然后在点击处理程序调用提交内部就像$('form').submit()这将触发表单上的提交

答案 1 :(得分:0)

在click事件上,您在提交事件上设置处理程序,但实际上您没有提交表单。试试这个:

$('form').submit(function() {  
    alert($(this).serialize()); // check to show that all form data is being submitted
    $.post("serializearray.php",$(this).serialize(),function(data){
        alert(data); //post check to show that the mysql string is the same as submit                        
    });
    return false; // return false to stop the page submitting. 
});
$('p').click(function() {
    $('form').submit();
});