简体   繁体   English

selectedOptions.length在Internet Explorer中不起作用,但在chrome和firefox中可以正常工作吗?

[英]selectedOptions.length not working in Internet explorer but works properly in chrome and firefox?

I have been working with ui.multiselect.js which is just similar to HTML Select: 我一直在使用ui.multiselect.js ,它与HTML Select相似:

I wanted the length of the selected elements. 我想要选定元素的长度。 So i had used the below code: 所以我用下面的代码:

var selectedOption =  document.getElementById('animalList').selectedOptions.length;

But it seems selectedOptions.length is not working in IE but working properly in Chrome and Firefox 但是似乎selectedOptions.length在IE中不起作用,但在Chrome和Firefox中正常工作

Below are the alternatives i have already tried: 以下是我已经尝试过的替代方法:

var select = document.getElementById('animalList');
var len = select.options.length;

I get the result as 0. 我得到的结果为0。

$("#animalList :selected").length;

I get the result as 0. 我得到的结果为0。

To get the count of a select element using multiselect , make sure your code looks like the following : 要使用multiselect获得select元素的数量,请确保您的代码如下所示:

 $(document).ready(function() { $('button').click(function() { // jquery version var count = $("#foo :selected").length; console.log("jQuery Count: " + count); // pure javascript version var options = document.getElementById('foo').options, count = 0; for (var i=0; i < options.length; i++) { if (options[i].selected) count++; } console.log("Pure Javascript: " + count); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Count Selected</button><br> <select multiple id="foo"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> 

I have included both a pure javascript version and jQuery version which are executed when you click the button. 单击按钮时,将同时执行纯JavaScript版本和jQuery版本。

You can get the length using the following method 您可以使用以下方法获得长度

  var len = 0;
  if($("#animalList").val()!=null){
   len = $("#animalList").val().length;
  }

WORKING FIDDLE 工作场所

暂无
暂无

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

相关问题 JS幻灯片可以在Firefox和Chrome上运行,但不能在Internet Explorer上运行吗? - JS slideshow works on firefox and chrome but not internet explorer? 网站可以在Firefox和Chrome中运行,但不能在Internet Explorer中运行 - Site works in Firefox and Chrome but not Internet Explorer 为什么这在fireFox中有效,但在Chrome或Internet Explorer 9中却无效? - Why is this working in fireFox but not in Chrome or Internet Explorer 9? Javascript代码段无法在Chrome中运行,但可在Internet Explorer中使用 - Javascript snippet not working in Chrome, but works in Internet Explorer 禁用复选框,该复选框不适用于Internet Explorer,但适用于Chrome - Disable checkboxes not working in Internet explorer but works in chrome 提交按钮不适用于Chrome或Firefox,但适用于Internet Explorer - The Submit Button doesn't work in Chrome or Firefox, but Works in Internet Explorer 难以在Internet Explorer中获取元素-适用于Chrome,Firefox - Having trouble getting elements in internet explorer — works with Chrome, Firefox 在Chrome中工作,但在Internet Explorer中不工作 - Works in Chrome but not in Internet Explorer javascript脚本可在Firefox,Chrome,Safari,Internet Explorer &lt;9,而不是IE 9中运行 - javascript script works in Firefox, Chrome, Safari, Internet Explorer < 9, but not in IE 9 Facebook连接。 适用于Firefox / Internet Explorer,不适用于Chrome / Safari / Opera - Facebook connect. Works in Firefox/Internet Explorer, not in Chrome/Safari/Opera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM