遍历AJAX响应& div,将div值设置为等于相同id的AJAX值 [英] loop through AJAX response & divs, setting div values equal to AJAX values of same id

查看:258
本文介绍了遍历AJAX响应& div,将div值设置为等于相同id的AJAX值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用jQuery,我已经能够最大程度地放松自己的懒惰,但真正的好处是将大部分时间花在了好东西上,而把时间花在了乏味上.我想进一步.

Because of jQuery, I've been able to indulge my laziness to the max, but the real benefit has been spending most time on the good stuff and less on the tedious. I'd like to further that.

当前,我将有一些类似这样的HTML:

Currently, I'll have some HTML like so:

<div id="id1"></div>
<div id="id2"></div>
<div...

以此类推.

然后,我将调用ajax,期望对象"ids"与div id相同,如下所示:

Then, I'll call ajax, expecting the object "ids" to be the same as the div ids like so:

$.ajax({
    success: function (msg) {
        $("#id1").text(msg.id1);
        $("#id2").text(msg.id2);
        $("#...

以此类推.

这可以简化为单线"吗?具体来说,我不知道如何在响应对象数组中使用div的ID.

Can this be reduced to a "one-liner"? Specifically, I don't know how to use the ids of the divs in the response object array.

非常感谢!

推荐答案

是的,可以使用 jQuery.each() 方法:

Yes, it can, use the jQuery.each() method:

success: function (msg) {
  jQuery.each(msg, function(key, value){
    $('#' + key).text(value);
  });
}

这篇关于遍历AJAX响应&amp; div,将div值设置为等于相同id的AJAX值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆