jquery - 从 jQuery ajax 响应 html 更新多个 div

标签 jquery asp.net-mvc ajax

我在 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/

相关文章:

javascript - 第二个 ajax 调用在 Phonegap Jquery 移动 Windows 手机 8 中不起作用

jquery - 从 JSON 构建菜单

使用按钮名称重置 JavaScript

jquery - 字段日期必须是chrome中mvc中的日期

javascript - 从 AJAX 调用获取结果到 javascript 变量

javascript - 为什么 XMLHttpRequest 会覆盖现有事件?

javascript - Jquery OnLoad - 奇怪的行为

javascript - 为什么这个函数返回 true 而不是 false

asp.net - 区域的 RedirectToAction?

c# - 如何从同一项目中的另一个 Api Controller 调用 Api Controller