I need to store a value in attribute or button it doesn't matter and i need to get that value again.
My approach that i need when any one click that link or button i will get the value and do some staff.The problem here is that i can't get the value again.
Here is what did :
$.each(result[1], function (index, val) {
$("<a href='#' class='track_it' val=" + val.locations + ">CLICK !</a>").appendTo(".my_data_table");
});
Where locations is a list of dicts
sorry i'm pythonic.
when i try to do this :
$(".track_it").click(function () {
console.log($(".track_it").attr("value"));
});
i do not get the value right, just something like [objc
it's string ?
答案 0 :(得分:1)
在jQuery中,您可以使用SomeApplication.exe /?
属性来设置新属性。语法也非常简单:data
,您可以使用$('.myElement').data('myAttribute', 'someValue')
你可以循环执行此操作,只需用循环替换循环中的行:
$('.myElement').data('myAttribute');
然后以这种方式检索它:
$.each(result[1], function (index, val) {
$("<a href='#' class='track_it' data-location=" + val.locations + ">CLICK !</a>").appendTo(".my_data_table");
});
答案 1 :(得分:0)
当你这样做时:
$(" <a href='#' class='track_it' val=" + val.locations + ">CLICK !</a>")
JavaScript将val.locations
转换为字符串。它变为[object Object]
,并成为<a>
HTML的一部分。
为避免这种情况,请使用jquery.data
。
$('<a ...stuff... />').data('val', val.locations).appendTo(".my_data_table")
要检索该值,只需再次使用jquery.data
,但不要使用第二个参数。
console.log($(".track_it").data('val'));