Javascript 如何将数组传递给 jQuery .data() 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6071618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an array into jQuery .data() attribute
提问by wilsonpage
Ok so I want to pass a very basic array into a jquery data attrubute server side like so:
好的,所以我想将一个非常基本的数组传递到 jquery 数据属性服务器端,如下所示:
<div data-stuff="['a','b','c']"></div>
and then retreive like so:
然后像这样检索:
var stuff = $('div').data('stuff');
alert(stuff[0]);
Why does this appear to alert '[' and not 'a' (see JSfiddle link)
为什么这似乎提醒“[”而不是“a”(参见 JSfiddle 链接)
JSFiddle Link :http://jsfiddle.net/ktw4v/3/
JSFiddle 链接:http : //jsfiddle.net/ktw4v/3/
回答by Alnitak
It's treating your variable as a string, the zeroth element of which is [
.
它将您的变量视为一个字符串,其中第零个元素是[
.
This is happening because your string is not valid JSON, which should use double-quotes as a string delimiter instead of single quotes. You'll then have to use single-quotes to delimit the entire attribute value.
发生这种情况是因为您的字符串不是有效的 JSON,它应该使用双引号作为字符串分隔符而不是单引号。然后,您必须使用单引号来分隔整个属性值。
If you fix your quotation marks your original code works (see http://jsfiddle.net/ktw4v/12/)
如果您修复引号,您的原始代码将起作用(请参阅http://jsfiddle.net/ktw4v/12/)
<div data-stuff='["a","b","c"]'> </div>
var stuff = $('div').data('stuff');
When jQuery sees valid JSON in a data attribute it will automatically unpack it for you.
当 jQuery 在数据属性中看到有效的 JSON 时,它会自动为您解包。
回答by John Green
Declaring it as an attribute means that it is a string.
将其声明为属性意味着它是一个字符串。
So stuff[0]
would be equivalent to: var myString = "['a','b','c']"; alert(myString[0]);
所以stuff[0]
相当于:var myString = "['a','b','c']"; alert(myString[0]);
You need to make it look like this:
你需要让它看起来像这样:
<div data-stuff="a,b,c"></div>
var stuff = $('div').data('stuff').split(',');
alert(stuff[0]);
Retraction: jQuery's parsing fails because it didn't meet the rules of parseJSON.
撤回:jQuery 的解析失败,因为它不符合 parseJSON 的规则。
However, I will stand behind my solution. There are aspects of the others that are less than ideal, just as this solution is less than ideal in some ways. All depends on what your paradigms are.
但是,我将支持我的解决方案。其他方案的某些方面并不理想,就像此解决方案在某些方面不太理想一样。一切都取决于你的范式是什么。
回答by Raja
As others have identified the value is treated as string so it is returning "[". Please try this (aaa is the name of the div and I took out the data-stuff):
由于其他人已经确定该值被视为字符串,因此它返回“[”。请试试这个(aaa 是 div 的名称,我取出了数据):
$(function(){
$.data($("#aaa")[0],"stuff",{"aa":['a','b','c']});
var stuff = $.data($("#aaa")[0],"stuff").aa;
alert(stuff[0]); //returns "a"
});
回答by Mayank
A different approach is posted at jsfiddle; var stuff = $('div').data('stuff');
stuff is a string with 0th character as '['
jsfiddle发布了一种不同的方法;var stuff = $('div').data('stuff');
stuff 是第 0 个字符为'['的字符串
Well, var stuff = eval($('div').data('stuff'));
should get you an array
好吧,var stuff = eval($('div').data('stuff'));
应该给你一个数组