我在 asp.net mvc 购物车中有一个页面,允许您输入优惠券并显示订单摘要以及其他页面内容。我想要一个“更新”按钮,该按钮将验证优惠券代码、报告任何错误并通过 jQuery ajax 更新页面上的订单摘要。
我知道我可以通过制作表单和部分 View 并在 jQuery 提交中使用目标属性来做到这一点。然而,我想我可以做如下的事情:
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
这里的优点是我不必刷新整个页面或创建包含所有需要更新的区域的部分 View 。有没有合适的方法通过 jQuery ajax 来做到这一点?
最佳答案
可以说是比 James Skidmore's answer 更干净的解决方案,虽然想法是一样的:
var $holder = $('<div/>').html(responseHtml);
$('#coupon').html($('#coupon', $holder).html());
$('#order-summary').html($('#order-summary', $holder).html());
关于jquery - 从 jQuery ajax 响应 html 更新多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/963386/