如何在jQuery中选择data-id和data-action

时间:2011-09-02 03:04:05

标签: jquery jquery-selectors

这是对此问题的跟进:Using javascript:function syntax versus jQuery selector to make Ajax calls

我如何选择这个片段?

<div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div>

我有以下的id值并尝试了这个

 $('.add-to-list[data-action|="action-follow-global-id"][data-id|=id]').text('here is the things jtjt in the id value');

但没有骰子。需要和他们在一起。

请求帮助

4 个答案:

答案 0 :(得分:21)

没有测试,但应该有效:

var id = 54;
$('.add-to-list[data-action=follow-global-id][data-id='+id+']').text('something');

答案 1 :(得分:2)

这将有效:

$('.add-to-list[data-action="follow-global-id"][data-id="54"]').
    text('here is the things jtjt in the id value');

以下是您可以执行以测试的完整代码示例:

<html>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js"></script>
    <script>
    $(function(){
        $('.add-to-list[data-action="follow-global-id"][data-id="54"]').
            text('here is the things jtjt in the id value');
    });
    </script>
    <div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div> 
</html>

答案 2 :(得分:0)

这是你要找的吗?

$('.add-to-list[data-action|="follow-global-id"][data-id]').each(function (i) {
    $(this).text('here is the things ' + $(this).attr('data-id') + ' in the id value');
});

答案 3 :(得分:-1)

您需要做的就是

$("[data-action=value")

OR

$("[data-id=value"])