2012-02-09 74 views
1

我有一個很多選擇的形式。如何獲取表單中每個選擇的選定值?

在發送表單之前,我需要檢查所有select是否有選項值。

我有,但沒有成功:

$('#myForm').submit(function() { 

$("#myForm select:selected").each(function(index){ 
    alert(index + ': ' + $(this).val()); 
}); 

}); 

怎麼辦呢?

回答

4

嘗試使用:

$("#myForm select").each(function(index){ 
    if ($(this).has('option:selected')){ 
     alert('Select number ' + index + ': ' + $(this).val()); 
    } 
}); 
+0

完美!謝謝 – bahamut100 2012-02-09 14:59:23

1

你將不得不做這樣的:

$("#myForm select:selected").each(function(index){ 
    alert(index + ': ' + $(this).val()); 
}); 

這會提醒你myForm會選擇的所有選定值。

+1

它不工作:/ – bahamut100 2012-02-09 14:55:04

+0

我們展示你的HTML代碼,因爲沒有看到它,這是一個有點難以確定最佳的選擇! – 2012-02-09 14:56:28

0
var valid = $("select #myForm").filter(function(index){ 
       return !$(this).val(); 
      }).length == 0; 

alert(valid) 
相關問題