0

我想通过单击提交按钮以外的元素来提交表单,在这种情况下是

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

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. 
});
});

})

4

2 回答 2

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()这将触发表单上的提交

于 2013-06-28T21:36:24.930 回答
0

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

$('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();
});
于 2013-06-28T21:37:22.143 回答