2009-08-05 29 views
18

Mam trochę kodu, który używa selektora w pętli.jQuery dynamiczny selektor

to działa:

document.getElementById("new_grouping_"+i).value 

To nie: $("#new_grouping_"+i).value

Czy istnieje sposób to zrobić przy użyciu jQuery?

Odpowiedz

21

Należy użyć val() funkcję:

var myValue = $("#new_grouping_"+i).val(); // to get the value 

$("#new_grouping_"+i).val("something"); // to set the value 
11

$("#new_grouping_"+i).val() dostaje wartość formularza.
$("#new_grouping_"+i).text() pobiera tekst elementu html.
$("#new_grouping_"+i).html() dostajesz html elementu html.

$("#new_grouping_"+i).val('value') Ustawia wartość formularza.
$("#new_grouping_"+i).text('value') Ustawia tekst elementu html.
$("#new_grouping_"+i).html('value') Ustawia html elementu html.

$("#new_grouping_"+i).append('value') poprzedza coś na początku elementu $("#new_grouping_"+i).append('value') dopisuje coś na końcu elementu

$("#new_grouping_"+i).before('value') miejsca coś przed elementem $("#new_grouping_"+i).after('value') miejsc czegoś po elemencie.

Zobacz więcej: jQuery Manipulation

+0

Firefox daje mi: "$ (" # new_grouping_desc _ "+ i) .Value nie jest funkcją" kiedy dodać pusty paren po wartości –

+2

Zaktualizowano *. Była dwie litery od rzeczywistej funkcji. –