2013-11-21 39 views
-1

我有5個選擇框,我想要獲得所有選項選擇值到這樣的變量:val + val + val + val
下面你可以看到我的代碼,但它不是完整的工作因爲如果我沒有選擇,我得到+++++jQuery多個選擇選項搜索結果

if($('select').attr("selectedIndex") == 0) { 
     var allVariables = $("select option:selected").map(function(){ 
      return this.value 
     }).get().join("+"); 
    } 

我缺少什麼?

回答

1

它返回空值,因爲你的選擇選項報頭還是代表用戶的選擇,他們只是沒有任何價值。所以加在某個值的檢查你回到它之前:

$('.find').click(function(e){ 
var Scat = $('#Scat option:selected').val(); 
var allVariables = $("select option:selected").map(function(){ 
    if(this.value!=""){ //only return if we have a value 
    return this.value; 
    } 
}).get().join('+'); 

alert(allVariables); 
if(Scat == ""){ 
    alert("Choose a Category"); 
} else { 

    // do something else 
}; 
}); 
+0

*我想獲得的所有選項選擇的值* - 我認爲他們需要選擇 – George

+0

@oGeez「,但它不是完整的工作,因爲如果我有沒有選擇我得到+++++「 –

+0

事實上,我認爲你是正確的,這個問題措辭笨拙 - 道歉。 – George