2

我一直在尝试向我的按钮添加一个警报,以更新数据库中的用户详细信息。

我尝试将 onclick 方法直接添加到按钮并使用函数,但它似乎不起作用。

我的按钮是;

<input type="submit" id="profileclick" value="Update" class="button-link"/>

我通过以下方式提交表格:(如果它很重要)

<form id="profile" method="post" action="../script/updateUserDetails.php">

我尝试的方法之一是

$('#profileclick').click(function(){
 alert('Your details have been updated');
 $('#profile').submit();
});

在所有情况下,详细信息都会更新,但我没有收到警报。

4

2 回答 2

3
$('#profileclick').click(function(){    
     alert('Your details have been updated');
     $('#profile').submit();
});


$('#profile').submit(function( e ){
         e.preventDefault();

         // ........ AJAX SUBMIT FORM
});

或者只是在提交之前添加延迟使用setTimeout...

$('#profileclick').click(function(){    
     alert('Your details have been updated');
     setTimeout(function(){
             $('#profile').submit();
     }, 2000);        
});
于 2013-01-07T20:12:07.713 回答
0
$('#profileclick').click(function(e) {
    e.preventDefault(); // prevents the form from being submitted by the button
    // Do your thing
    $('#profile').submit(); // now manually submit form
});

编辑:

只是一个注释。这不会阻止通过其他方式提交表单,例如在文本字段中按 Enter。为了防止表单被提交,您必须在表单本身上使用 preventDefault() ,如另一个答案中给出的那样。

于 2013-01-07T20:30:03.557 回答