[英]how to turn off reload of page after sending ajax to php and how to fix this code
將AJAX發送到PHP后如何關閉頁面重新加載? 每次按提交時,頁面都會重新加載。 是否可以發送一些沒有表格的圖片?
<script>
$(document).ready(function (e){
$("#uploadForm").on('submit', (function(e){
e.preventDefault();
$.ajax({
url: "/class/pdo/add_new_field.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#targetLayer").html(data);
},
error: function(){}
});
}));
$('#uploadForm').submit(function () {
return false;
});
});
</script>
<form id="uploadForm" method="POST">
<div style="margin-left: 75px;">
<div class="save_divs">picture<input type="file"></div>
<div class="save_divs">name:</div>
<div class="save_divs">date</div>
<div class="save_divs">short_info</div>
<div class="save_divs">long info<input type="submit"></div>
</div>
</form>
我需要在$.ajax
網址中使用斜杠( /
)嗎?
sendContactForm();
很重要 如果是,請用它更新您的問題。 )
從}));
像這樣
<script>
$(function(){
$("#uploadForm").on('submit',function(e){
e.preventDefault();
$.ajax({
url: "/class/pdo/add_new_field.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#targetLayer").html(data);
},
error: function(){}
});
}); // also a ) too many here
});
</script>
好的,第一個問題是:-
您已經使用on
綁定了兩次提交,一次使用了submit()
綁定一次,僅使用了一次。
現在由於第二個綁定而出現錯誤,sendContactForm()中存在一些錯誤,由於該錯誤永遠不會return false
到該行,因此return false
並且默認頁面已提交並重新加載,還檢查控制台錯誤,以防止混淆將來,您可能會使用e.preventDefault();
在功能開始時停止進一步傳播。 (感謝@rory)
$('#uploadForm').submit(function (e) { e.preventDefault(); sendContactForm(); return false; });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.