How to pass an array into jQuery .data() attribute

前端 未结 4 2040
野的像风
野的像风 2020-12-13 03:39

Ok so I want to pass a very basic array into a jquery data attrubute server side like so:

<
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 03:44

    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]);

    You need to make it look like this:

    var stuff = $('div').data('stuff').split(','); alert(stuff[0]);

    Retraction: jQuery's parsing fails because it didn't meet the rules of 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.

提交回复
热议问题