2011-04-25 29 views
1
<span> 
    <label class="label">Color</label> 
    <span class="foo"><input name="Color" value="Blue" class="customs" maxlength="100" type="text"/></span> 
    </span> 
</span> 

<span> 
    <label class="label">Brand</label> 
     <span class="input-large"><input name="Brand" value="xxx" class="customs" maxlength="100" type="text"/></span> 
    </span> 
</span> 

我想要使用JS的所有輸入值。 我寫了使用JS捕捉多個值

$('.customs').each(function() { 

    alert($('.customs').val()); 

}); 

但每次它給我第一個輸入值。

Required Alert output: Blue, Brand 
Output comes: Blue, Blue 

回答

1

更換

alert($('.customs').val()); 

在你的兩個輸入

alert($(this).val()); 
0

雙重校驗值與名稱定義

0

您正在評估在每次迭代的選擇,我想當在數組上調用val時,它會選擇第一個值。

使用

$('.customs').each(function() { 
    alert($(this).val()); 
}); 

甚至更​​好,

$('.customs').each(function() { 
    alert(this.value); 
}); 

,因爲沒有必要通過val這裏提供的層。

0

由於您將類用作選擇器,因此您將獲得一個集合。您可以通過將其放入數組或迭代它來完成某些操作。

$('.customs').each(function() { 
    alert($(this).val());//use this to refer to the current object 
});