2011-09-02 50 views
14

这是一个后续行动这样一个问题:Using javascript:function syntax versus jQuery selector to make Ajax calls如何选择数据-ID和数据在行动jQuery的

我怎么会选择这个片段?

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

,但没有骰子。需要与这些在一起。

THX的帮助

回答

20

没有测试,但应该工作:

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

我做了测试:http://jsfiddle.net/ambiguous/8jheS/ –

+0

的伟大工程 - THX!必须等待才能接受答案 – timpone

+2

虽然没有它们可能会起作用,但请注意,属性值周围的引号是*强制*。 –

1

这将工作:

$('.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> 
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'); 
}); 
-1

所有你需要做的是

$("[data-action=value") 

OR

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

尽管此代码可能会回答问题,但提供有关* why和/或* how *这个代码如何回答问题的其他上下文可以提高其长期价值。 –