javascript - 提交后,输入文本消失并重新加载页面(AJAX)
问题描述
为什么我的页面即使我使用了 ajax 也会重新加载,并且在单击提交按钮后它也会消失我的输入文本。我已经使用 show 来解决它,但它不起作用。
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<input type="text" name="name" id="name">
<span id="namenotif" style="color:red;"> <span>
<br>
<input type="text" name="price" id="price">
<span id="pricenotif" style="color:red;"> <span>
<br>
<input type="submit" name="submit" id="save"><br>
</form>
<script>
$(document).ready(function() {
$(document).on("click","#save",function(){
var name = $("#name").val();
var price = $("#price").val();
if(name==""){
$("#namenotif").html("Enter a name");
$("#name").show("fast");
$("#save").show("fast");
}
else if(price==""){
$("#pricenotif").html("Enter a price");
$("#price").show("fast");
$("#save").show("fast");
}else{
$.ajax({
url:"addproduct.php",
type:"POST",
data:{name:name,price:price},
success:function(data){
alert("Successful");
}
});
}
});
});
</script>
解决方案
将 return false 添加到函数末尾,用于处理单击事件
推荐阅读
- python - 有人可以解释这段代码中实际参数和形式参数背后的原因吗
- apache-flink - Apache Flink 中是否有任何触发器每分钟触发一次延迟数据?
- python-3.x - 带有 ProcessPoolExecutor 的芹菜返回 ModuleNotFoundError
- python - 如何在 facebook 先知中获取有关变化点的值?
- angular - 如何使用 api 以材料的角度编写自动完成功能
- azure - 有没有办法从 Azure Runbook 进行 API 调用?
- python - 如何在python中使用扩展正则表达式?
- database - 将数组内的不同数量的值相乘:Mongodb
- android - 单击gridview中的项目时如何获取选定的列值?
- python-2.7 - 根据值内的条件对(有序)字典的值进行分组