1 回答

TA貢獻1847條經驗 獲得超7個贊
如果我理解您的問題,您只想更新當前頁面的一部分。如果是這樣,您將不得不為此使用 AJAX:
保留“提交”按鈕,但將其設為標準按鈕并為其指定一個 ID,例如“提交”:
<button id="submit" name="submit" title="add to cart">Add to Cart</button>
然后您的 JavaScript 將按如下方式處理按鈕上的點擊事件:
$(function() {
let submit = $('#submit');
submit.click( function() { //
submit.prop('disabled', true); // prevent a re-submission
var form = $('#ajax');
var request = $.ajax({
url: form.attr('action'), // get the action from the form
type: form.attr('method'), // get the method from the from
dataType: 'html', // the assumption is that we are dealing HTML here
data: form.serialize()
});
request.done(function(ajaxResult) {
// update the DOM with the results
$('#some_div').html(ajaxResult); // replace contents of <div id="some_div"></div> with new html
submit.prop('disabled', false); // re-enable the submit
});
});
});
您必須安排發回的結果只是需要更新的 HTML。
更新
自回復以來,您添加了一條帶有鏈接的評論,表明我可能誤解了您的意圖。您使用的短語“提交表單但在提交時阻止它打開新頁面”絕對可以使人理解我的原始解釋。
添加回答
舉報