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。
更新
自回复以来,您添加了一条带有链接的评论,表明我可能误解了您的意图。您使用的短语“提交表单但在提交时阻止它打开新页面”绝对可以使人理解我的原始解释。
添加回答
举报