2011-09-02 2 views
14

Questo è un follow-up a questa domanda: Using javascript:function syntax versus jQuery selector to make Ajax callscome selezionare i dati-id e data-azione in jQuery

Come potrei selezionare questo frammento?

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

ho il valore id sottostante e provato questo

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

ma nessun dadi. Bisogno di essere AND'ing questi insieme.

thx per aiuto

risposta

20

non prova, ma dovrebbe funzionare:

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

ho fatto prova che: http://jsfiddle.net/ambiguous/8jheS/ –

+0

grandi opere - thx! attendere per accettare la risposta – timpone

+2

Anche se potrebbe funzionare senza di essi, si noti che le virgolette intorno ai valori degli attributi sono * obbligatori *. –

1

Ciò funzionerà:

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

Ecco un esempio di codice completo è possibile eseguire a prova:

<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

È questo che sei stai cercando?

$('.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

tutto quello che dovete fare è

$("[data-action=value") 

O

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

Mentre questo codice può rispondere alla domanda, fornendo un contesto aggiuntivo riguardo * perché * e/o * come * questo codice risponde alla domanda, migliora il suo valore a lungo termine. –