2016-09-18 107 views
0

我有一個多選字段,用戶可以選擇多個字段。現在它只取得下拉的第一個值。如何選擇多個值?

這裏是使用速度模板作爲前端jQuery代碼:

jQuery('select[name="myServices"]').live('change', function(ev) { 
      var selectedmyServicesOpts = jQuery(this).find(':selected'), 
        ismyServicesOther=false; 
      console.log("Getting all selectedvalue" + selectedmyServicesOpts) 
      if(selectedmyServicesOpts.length > 0) { 
       jQuery.each(selectedmyServicesOpts, function(index, value) { 
        if(jQuery(value).val() === 'Other (Text field)') { 
         jQuery('.myServicesOther').show(); 
         ismyServicesOther=true; 
         return false; 
        } 
       }); 
      } 
      if (!ismyServicesOther) { 
       jQuery('.myServicesOther').hide(); 
      } 
     }); 

回答

1

對於給定的<select multiple>元件,jQuery("theelement").val()給出所選的選項值的陣列。

在你的情況,你可能要檢查:

var values = jQuery(this).val(); 
if(values.indexOf('Other (Text field)') > -1) { 
    jQuery('.myServicesOther').show(); 
} 
else { 
    jQuery('.myServicesOther').hide(); 
} 
+0

現在我可以看到,這是越來越轉換爲一個數組,但是當我提交按鈕單擊它最終被證明是myServices = 1st + value&myServices = 2nd + value –

+0

正如我希望它們以數組形式出現 –

+0

根據您的服務器端語言,您需要在您的HTML和PHP中使用'name =「myServices []」'例如這會給你一個服務器上的數組。 –