ここに、ユーザーが値を入力する必要があるこのフォームがあります。
<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);
}
});
})
})
しかし、ボタンを2回クリックすると、リクエストを送信する代わりにページがリロードされます