[英]AJAX response using HTML5 data attribute
I've a working env where I'm using AJAX response to fill in HTML elements. 我有一个工作环境,我正在使用AJAX响应来填充HTML元素。
for example AJAX response has two(or n) objects like this: 例如,AJAX响应有两个(或n个)对象,如下所示:
0:Object
id: "111"
Name: "abc"
1:Object
id: "112"
Name: "xyz"
Then, There already would be two(or n) divs with user
class and HTML5 data-user
containing the id in HTML 然后,已经有两个(或n)div
user
类和HTML5 data-user
包含HTML中的id
<div class='user' data-user='111'>
<div class='userId'data-userId='111> </div>
<div class='usernm' data-user='usernm'> </div>
</div>
<div class='user' data-user='112'>
<div class='userId'data-userId='112> </div>
<div class='usernm' data-user='usernm'> </div>
</div>
What I need is put those response values in this divs like this: 我需要的是将这些响应值放在这个div中,如下所示:
<div class='user' data-user='111'>
<div class='userId'data-userId='111> 111 </div>
<div class='usernm' data-user='usernm'> abc </div>
</div>
<div class='user' data-user='112'>
<div class='userId'data-userId='112> 112 </div>
<div class='usernm' data-user='usernm'> xyz </div>
</div>
What I'm currently doing (and is working) is using jQuery find (see below code) but now I'm suggested to put the responses using HTML5 data-..
attribute. 我目前正在做的(并且正在工作)是使用jQuery find (参见下面的代码),但现在我建议使用HTML5
data-..
属性来放置响应。 I can't get around it, if someone can help up with it.. 如果有人可以帮助我,我无法解决它。
$.ajax({
type: 'GET',
url: '/url/goes/here',
dataType: "json",
success: function(data) {
$('.user').each(function(key, value){ //i need to remove .user and use data-user here (if possible)
$(value).find('.userid').text(data[key].id); //i need to put values using data attr instead of find
$(value).find('.usernm').text(data[key].name); //i need to put values using data attr instead of find
});
}
});
Data attributes are accessed by: 数据属性由以下方式访问:
$(#id).data('userId');
or 要么
$(#id).attr('data-userId');
and to set a value in that data attribute: 并在该数据属性中设置一个值:
$(#id).data('userId', 'value');
or 要么
$(#id).attr('data-userId', 'value');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.