![](/img/trans.png)
[英]HTML/Javascript: How to control refresh action without re-submit the form
[英]ajax form won't re-submit without page refresh
這里有點神秘的問題...
我在提交表單的頁面上建立了一個按鈕。 該表單嵌入了DIV,該DIV將更新以顯示修改后的數據。
(位於智能模板中)
形成:
<form id='itempost_{$item.itemid}_{$item2.itemid}' method='post' class='itempost'>
<input type='hidden' name='item' value='{$item.itemid}'>
<input class='btn_text' type="submit" value="Send Item">
</form>
JS
$('.itempost').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url : 'item.php',
data: $(this).serialize(),
success : function (response) {
$("#update").eq(0).load("update.php?&t={$parentid}");
},
error: function (jXHR, textStatus, errorThrown) {
alert('not ok');
alert(errorThrown);
}
});
return false;
});
update.php
update.php包含的所有內容都是重建頁面所需的最低要求,然后它將重建位於div #update中的模板部分。
$smarty->display('items.tpl');
第一次單擊該按鈕,效果很好,並且HTML內容發生更改,而對頁面沒有任何明顯影響。
但是,第二次點擊似乎沒有到達JS的任何地方,只是重新加載了頁面。 接下來的單擊將按預期方式工作,使我相信這些操作是:
第一次單擊:使用設計的AJAX提交表單第一次單擊:使用設計的AJAX提交表單第三次單擊:使用設計的AJAX提交表單第四次單擊:刷新頁面
等等
對我在這里做錯的任何想法嗎? 我已確保所有情況下均使用JS,檢查所有內容,並用手指搜索一下數字-我只是看不到哪里出錯了?
輝煌。
非常感謝yuriy636提供了答案,並感謝putvande幫助我整理了第二通電話。
現在,所有功能都可以完美運行
$(document).on('submit', '.itempost', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url : 'item.php',
data: $(this).serialize(),
success : function (response) {
$("#update").load("update.php?&t={$parentid}");
},
error: function (jXHR, textStatus, errorThrown) {
alert('not ok');
alert(errorThrown);
}
});
return false;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.