2016-09-18 84 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 []”'例如这会给你一个服务器上的数组。 –