gpt4 book ai didi

javascript - 使用ajax提交表单后表单消失

转载 作者:行者123 更新时间:2023-11-28 01:37:21 25 4
gpt4 key购买 nike

我有一个 html 表单,如下所示:

<form role="form" name="login" id="login" >


<div id="alert-success" class="alert alert-success" style="display: none" ></div>
<div id="alert-danger" class="alert alert-danger" style="display: none" ></div>

<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="name" class="form-control" id="username" placeholder="Enter name" style="width: 30%;">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="usermail" placeholder="Enter email" style="width: 30%;">
</div>

<div class="form-group">
<label for="exampleInputPassword1">Username</label>
<input type="password" class="form-control" id="loginname" placeholder="username" style="width: 30%;">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="loginpassword" placeholder="Password" style="width: 30%;">
</div>


<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" id="submit" data-dismiss="alert" name="submit" class="btn btn-default">Join Us</button>
</form>

同一文件中的 jquery 用于验证和提交表单:

$(document).ready(function () {
$("#submit").click(function () {
var username = $("#username").val();
var usermail = $("#usermail").val();
var loginname = $("#loginname").val();
var loginpassword = $("#loginpassword").val();
var emailregex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (username == "" || username.length > 20 || !isNaN(username)) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Proper Name");
$("#username").focus();
return false;

} else if (usermail == "" || !emailregex.test(usermail)) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Your Email Id");
$("#usermail").focus();
return false;

} else if (loginname == "" || loginname.length > 10) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Username");
$("#loginname").focus();
return false;

} else if (loginpassword == "" || loginpassword.length > 10) {

$('#alert-danger').show();
$("#alert-danger").html("Please Enter Proper Password");
$("#loginpassword").focus();
return false;

} else {
var datastring = 'name=' + username + '&email=' + usermail + '&loginusername=' + loginname + '&loginuserpassword=' + loginpassword;

$.ajax({
type: "POST",
url: "userdata.php",
data: datastring,
cache: false,
sucess: function (dataitem) {
if (dataitem == 1) {
$("#alert-danger").html("Something Went Wrong");
} else {
$("#alert-danger").html("Something Went Wrong");
}
}
})
}
})
})

现在我在提交表单时遇到问题,它确实有效,因为数据保存到数据库表中,但提交后我看不到我的表单,我使用了 location.reload(); 功能但仍然无法正常工作,请帮忙

最佳答案

这样做:

$("#submit").click(function() {

// your code


return false;
});

在其他部分

else

{
var datastring = 'name=' + username + '&email=' + usermail + '&loginusername=' + loginname + '&loginuserpassword=' + loginpassword;

$.ajax({
// code ....
});
return false;

}

当您正确填写表单后,它会通过ajax提交,然后通过点击提交。返回 false 停止下一次提交,只有您的 ajax 帖子会被发送。

关于javascript - 使用ajax提交表单后表单消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com