我在asp.net mvc购物车中有一个页面,允许您输入优惠券并显示订单摘要以及其他页面内容.我想有一个更新按钮,它将验证优惠券代码,报告任何错误,并通过jQuery ajax更新页面上的订单摘要
我知道我可以通过制作表单和部分视图并在jQuery提交中使用target属性来完成此操作.但是,我以为我可以做以下事情:
var options
{
success: function (responseHtml) // contains the entire form in the response
{
// extract sub-sections from responseHtml and update accordingly
// update <div id="coupon"> with coupon div from responseHtml
// update <div id="order-summary> with order summary div from responseHtml
}
}
$('#my-form').ajaxSubmit(options); // submits the entire form
这里的优点是我不必进行整页刷新或创建包含所有需要更新的区域的部分视图.有没有一种合适的方法通过jQuery ajax来做到这一点?
比 James Skidmore’s answer更清晰的解决方案,虽然这个想法是相同的:var $holder = $('<div/>').html(responseHtml);
$('#coupon').html($('#coupon', $holder).html());
$('#order-summary').html($('#order-summary', $holder).html());
