【发布时间】:2012-12-26 03:20:05
【问题描述】:
我这里有一个要求用户输入值的表单。
<form id="result" name="result" action="/HelloStruts2/views/register.action" method="post">
<label>UserName</label>
<input type="text" name="userBean.username" value="" id="result_userBean_username"/>
<input type="submit" id="registerSubmit" value="Submit"/>
</form>
这是我的脚本。
$(document).ready(function(){
$('#registerSubmit').live('click',function(){
alert("XD");
$.ajax({
//this is the php file that processes the data and send mail
url: "register.action",
//GET method is used
type: "POST",
//pass the data
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
$('#result').html(html);
}
});
})
})
当我提交表单时,表单正在被成功验证,而不是发送请求,整个页面正在重新加载或刷新,如何防止这种情况发生?
更新 我已经用这个更新了我的脚本。
$(document).ready(function(){
$('#result').on('submit',function(e){
e.preventDefault();
$.ajax({
//this is the php file that processes the data and send mail
url: "register.action",
//GET method is used
type: "POST",
//Do not cache the page
cache: false,
//success
success: function (html) {
$('#content').html(html);
}
});
})
})
但是当我第二次单击按钮时,页面正在重新加载而不是发送请求
【问题讨论】:
-
提交表单的方式不止一种,所以请不要绑定到可能不会实际用于提交的按钮。
-
#result 是否存在于#content 中?
-
具有 id 结果的表单是否存在于具有 id 内容的任何元素内?
-
我是这么认为的,看看我的编辑。
标签: jquery ajax forms jsp request