简体   繁体   English

从中选择多个选项后触发javascript函数<select>标签

[英]Trigger a javascript function after select multiple option from <select> tag

I have a multi select tag is here.我有一个多选标签在这里。 I like to call a function after select multiple option from select tag.我喜欢在从 select 标签中选择多个选项后调用一个函数。

<select name="cars" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

If any body mark as duplicate please let me know the correct link.如果任何正文标记为重复,请告诉我正确的链接。 I search lots here and google also.我在这里和谷歌搜索了很多。 but non of solution is good for my problem.但非解决方案对我的问题有好处。

You can listen to change event in the same way as you do this with non-multiple select :您可以使用与非多select相同的方式来监听change事件:

 $("#cars").on('change', function() { var selectedItems = $(this).find("option:selected"); if (selectedItems.length <= 1) { $("#result").text("not multiple!"); return; } var text = ""; $.each(selectedItems, function() { text += $(this).val() + " "; }); $("#result").text(text); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="cars" name="cars" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> <div id="result"></div>

You can do something like this with help of change() and blur() event handler你可以在change()blur()事件处理程序的帮助下做这样的事情

 $('select').change(function() { if ($('option:selected', this).length > 1) { // set custom data attribute value as 1 when change event occurred and selected options are greater than 1 $(this).data('select', 1); } else //set value as zero in other case $(this).data('select', 0); }).blur(function() { if ($(this).data('select')) { // when focus out check custom attribute value alert('selected more than one'); // code here // call your function here } $(this).data('select', 0) // set custom attribute value to 0 });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select name="cars" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>

Use .change instead like so :使用.change代替像这样:

$('select').change(function(e){
  var length = $('option:selected', this).length;
  if ( length > 1 ) {
    alert('hey');  
  }
});

DEMO演示

try this, it will call a function passing the newly selected value into functionToCall .试试这个,它会调用一个函数,将新选择的值传递给functionToCall

var values = [];
$('select').change(function(e){

    var current = $(this).find('option:selected').map(function() {
        return $(this).val();
    });

    var newValue = $(current).not(values).get();
    values = current;

    if(newValue.length)
        functionToCall(newValue);
});

function functionToCall(value){
    alert(value);   
    console.log(value);
}

Fiddle小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM