gpt4 book ai didi

javascript - 不刷新页面的表单提交

转载 作者:IT王子 更新时间:2023-10-29 01:07:32 25 4
gpt4 key购买 nike

<分区>

也许有人可以帮助我解决我遇到的这个小问题。我试图在不刷新页面的情况下提交此表单。但它会跳过帖子并直接转到 ajax 调用。我想我想念 preventDefault() 的理解。我在网上搜索过,但找不到我需要的东西。非常感谢您的帮助或指向另一个表单提交。

下面的 HTML:

<!DOCTYPE html>
<html>
<head>
<title>AJAX | Project</title>
<link href="project.css" rel="stylesheet"/>
<script src="jquery.js"></script>

</head>
<body>


<div id="mainCon">

<h1>Contact Book</h1>
<div id="form_input">
<form id="myform" method="post" action="addrecord.php">

<label for="fullname">Name:</label>
<input type="text" name="fullname" id="fullname"/><span id="NameError"> </span>
<br/>
<label for="phonenumber">Phone Number:</label>
<input type="text" id="phonenumber" name="phonenumber"><span id="PhoneError"></span>

<br />

<input id="buttton" type="submit" onClick="addnumber()" value="Add Phone Number"/>
<input type="button" id="show" value="the Results"/>
</form>


</div>

<div id="form_output">


</div>


</div>
<script src="project.js"></script>
<script type="text/javascript">

function addnumber(){

var Fullname = document.getElementById("fullname").value;
var Phonenumber = document.getElementById("phonenumber").value;



if(Fullname == ""){
document.getElementById("NameError").innerHTML = "Please Enter a valided Name";
}

if(Phonenumber == ""){
document.getElementById("PhoneError").innerHTML = "Please Enter a valided Name";
}

}

</script>
</body>
</html>

j查询

$("document").ready(function () {
$("#buttton").click(function () {
$('#myform').submit(function (e) {
e.preventDefault();
$.ajax({
url: "listrecord.php",
type: "GET",
data: "data",
success: function (data) {
$("#form_output").html(data);
},
error: function (jXHR, textStatus, errorThrown) {
alert(errorThrown);
}
}); // AJAX Get Jquery statment
});
}); // Click effect
}); //Begin of Jquery Statement

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