[英]getting an attribute value using jquery
I am trying to get the 31003 value from the attribute with jquery. 我正在尝试使用jquery从属性中获取31003的值。
<a class= "findOption" data-clickaction="changeFilter" data-actionvalue=" {"Company":"31003", "val": "mostrecent";}" href="javascript:void(0);">Most Recent</a>
I have tried, 我努力了,
$(".findOptions").attr('Company').value(); $( “findOptions”)ATTR( '本公司')的值()。;
$(".findOptions").attr('data-actionvalue'); $( “findOptions。”)ATTR( '数据actionvalue');
I am stuck. 我被困住了。
You can use data method : 您可以使用数据方法 :
$('.findOption').data('actionvalue')['Company'];//findOption instead as noticed by @Vimalan
And as @Barmar indicated that html is invalid you should be doing like this in your html: 正如@Barmar指出html无效,您应该在html中这样做:
data-actionvalue='{"Company":"31003", "val": "mostrecent"}'
First, your HTML isn't correct. 首先,您的HTML不正确。 Since you're using double quotes in the JSON that's in data-actionvalue
, you have to use single quotes as the delimiter around the attribute value. 由于您在data-actionvalue
中的JSON中使用双引号,因此必须使用单引号作为属性值周围的定界符。 Otherwise, the quote before Company
will end the attribute. 否则, Company
前面的引号将结束属性。
Also, you shouldn't have a semicolon ( ;
)` in the value, that's not valid JSON. 另外,您不应在值中使用分号( ;
)`,因为这不是有效的JSON。
<a class= "findOption" data-clickaction="changeFilter" data-actionvalue='{"Company":"31003", "val": "mostrecent"}' href="javascript:void(0);">Most Recent</a>
Then to get a particular property of the data, you need to access it with .
然后,要获取数据的特定属性,您需要使用进行访问.
: :
$('.findOption').data('actionvalue').Company
or []
: 或[]
:
$('.findOption').data('actionvalue')['Company']
There are couple of corrections in your code: 您的代码中有几处更正:
1) Fix, 'actionvalue' as shown in my code. 1)修正“ actionvalue”,如我的代码所示。 2) Use findOption instead of findOptions (it does not exists in your code) 2)使用findOption代替findOptions(它在您的代码中不存在)
var obj = $(".findOption").data('actionvalue'); $('#result').text(obj.Company);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class= "findOption" data-clickaction="changeFilter" data-actionvalue='{"Company":"31003", "val": "mostrecent"}' href="javascript:void(0);">Most Recent</a> <br/> <label id='result'/>
我认为这应该工作
$(".findOption").data('actionvalue')['Company'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.