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

前端 未结 4 2032
野的像风
野的像风 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:55

    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.

    If you fix your quotation marks your original code works (see http://jsfiddle.net/ktw4v/12/)

    var stuff = $('div').data('stuff');

    When jQuery sees valid JSON in a data attribute it will automatically unpack it for you.

提交回复
热议问题