使用JQuery链接传递对象

时间:2022-06-29 20:35:39

I am using socket.io to receive a list of objects. I am then iterating though them to generate links. I need to display this object when a users selects one, but I dont know how to pass the object though the link?

我使用socket.io来接收对象列表。然后我迭代它们来生成链接。我需要在用户选择一个时显示该对象,但我不知道如何通过链接传递对象?

socket.on('result', function (result) {

for (var object in result.blue) {
   $('.res_section').append(
        '<a href="#" id="result_object"> ' + result.blue[object].name +'</a>');
         //I need to append result.blue[object];
    }
 });


 $(document).on("click", "#result_object", function(){
          //do something here with object
 });

object = {name: {first: "a_name", second: "second_name"}, age: {type: "number", value: "13"}}

1 个解决方案

#1


0  

The object had to be turned into a JSON object and then the quotations replaced with '&#x27'. It can then be used with .data();

必须将对象转换为JSON对象,然后将引号替换为“'”。然后它可以与.data()一起使用;

    data-object=JSON.stringify(object).replace(/\'/g,'&#x27;')

#1


0  

The object had to be turned into a JSON object and then the quotations replaced with '&#x27'. It can then be used with .data();

必须将对象转换为JSON对象,然后将引号替换为“'”。然后它可以与.data()一起使用;

    data-object=JSON.stringify(object).replace(/\'/g,'&#x27;')