1 回答

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果我理解您的問(wèn)題,您只想更新當(dāng)前頁(yè)面的一部分。如果是這樣,您將不得不為此使用 AJAX:
保留“提交”按鈕,但將其設(shè)為標(biāo)準(zhǔn)按鈕并為其指定一個(gè) ID,例如“提交”:
<button id="submit" name="submit" title="add to cart">Add to Cart</button>
然后您的 JavaScript 將按如下方式處理按鈕上的點(diǎn)擊事件:
$(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
});
});
});
您必須安排發(fā)回的結(jié)果只是需要更新的 HTML。
更新
自回復(fù)以來(lái),您添加了一條帶有鏈接的評(píng)論,表明我可能誤解了您的意圖。您使用的短語(yǔ)“提交表單但在提交時(shí)阻止它打開(kāi)新頁(yè)面”絕對(duì)可以使人理解我的原始解釋。
添加回答
舉報(bào)