我从php脚本中获取数据如下:
$.ajax({
url: './fetchData.php',
type: "POST",
data: {
mail: mail
},
dataType:'text',
success: function(ans)
{
var data = JSON.parse(ans);
$('ul.dropdown-alerts').html('');
$.each(data, function(i, v) {
$('ul.dropdown-alerts').append('<li><a href="#"><div><i class="fa fa-comment fa-fw"></i>' + v.Content + '<span class="pull-right text-muted small">' + v.Date + '</span></div></a></li>');
$('ul.dropdown-alerts').append('<li class="divider"></li>');
});
$('ul.dropdown-alerts').append('<li><a class="text-center" href="more.php"><strong>See More</strong></a></li>');
}});
但是,在我的json字符串中,我有Content
和Date
以上。一般来说,我有:
[{"cid":"1","Content":"esj","commercial_resource":"","Date":"2015-10-21 19:00:00","Price":"0.00"},{"cid":"1","Content":"esj","commercial_resource":"","Date":"2015-10-21 19:00:00","Price":"0.00"},
{"cid":"1","Content":"esj","commercial_resource":"","Date":"2015-10-21 19:00:00","Price":"0.00"},
{"cid":"1","Content":"esj","commercial_resource":"","Date":"2015-10-21 19:00:00","Price":"0.00"}, etc.]
当用户点击其中的超链接时,我想显示带有所选行的cid的警报。我该怎么办?
答案 0 :(得分:1)
与您稍后要使用的记录相关的任何信息,作为data- *元素存储在对象上。您可以稍后引用它并随意使用它。
就像您引用对象上的v.Content一样,您可以引用对象上的v.cid并将其直接放在链接上或者是父
'<a data-cid="'+ v.cid +'"></a>' <-- $(this).data('cid')
'<li data-cid="'+ v.cid +'"><a></a></li>' <-- $(this).closest('li').data('cid')
稍后给予对象事件访问的小例子......
var data = [
{ name: 'Me', location: 'Here' }
, { name: 'You', location: 'There' }
, { name: 'We', location: 'Internet' }
];
$('ul').append($.map(data, function(record){
var $li = $('<li>');
$li.text(record.name);
$li.on('click', function(){
console.log(record);
});
return $li;
}));