2011-07-20 20 views
27

我想獲取位於單個類名稱中的所有字段..例如我的代碼。獲取位於特定類別中的所有文本字段值

<div class="test"> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
</div> 

<div class="test"> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
<input type="text" class="text-field" /> 
</div> 

我想要得到的each loop在它返回我,就設在這個類中的文本字段值.. 任何建議.. (我在jQuery的是新)

回答

67

試試這個:

$(".test .text-field") 

編輯:

爲了獲取值試試這個:

$(".test .text-field").each(function() { 
    alert($(this).val()); 
}); 
+0

我想要每個文本字段的所有值。 – chhameed

+0

爲你做了編輯 –

+0

如果有人想要得到非輸入元素的價值,請使用'.html()'而不是'.val()' – blackandorangecat

0

你試過這樣的:

$(".test input[type=\"text\"]") 
0

從cwallenpoole的回答下面應該工作

$.each($(".test input[type=\"text\"]"), function(index, ele){ 
    alert(ele.val()); 
}); 
33

如果你想把所有的值到一個數組,你可以這樣做:

var texts= $(".test .text-field").map(function() { 
    return $(this).val(); 
}).get(); 
+3

真棒非常漂亮,漂亮,乾淨,...和 – doniyor

+1

我在找什麼。謝謝! –

+1

清潔和優雅! – Bhaskara

相關問題