我正在研究一個小插件,它檢查幾個輸入是否具有相同的值。我認爲最好的方法是將值存儲在一個數組中,並檢查這些值是否是唯一的,還是有更好的方法來做到這一點?JS:如何將數組中的值存儲在循環(每個)中並對其進行比較?
//想法
$('input').each(function(){
var type = $(this).attr('class');//can be a other attribute
switch(type){
case: 'red':
// some code
break;
case: 'green':
// some code
break;
case: 'black':
// the code to see if all inputs with the class black
//if they have the same value return in true or false, if all of the values
// are empty it should return false
break;
// more cases....
};
});
如果你想檢查輸入的*值*,爲什麼你的代碼看着「class」屬性?等一下;你想通過類來檢查類似的值......那麼,這將是一個非常脆弱的事情,因爲一個元素可以有多個「class」值;或者更恰當地說,「類」在語義上是一個*值列表*。 – Pointy 2012-01-06 14:39:55
「返回真假」是什麼意思? – Blazemonger 2012-01-06 14:40:08
這些輸入可以返回的數據類型是什麼? – 2012-01-06 14:43:05