Using the .val() function on a multi-select các mục will return an array of the selected values:var selectedValues = $("#multipleSelect").val();
và in your html:
what if u want lớn get Text 1 instead of value? replace .val() with .text()? – Raza Ahmed Sep 6 "13 at 6:17
Worth noting that a multiple select with nothing selected returns null rather than an empty array. This means if you’re programatically adding a selected value, you have sầu a bit of juggling to vì to get it right. – Leo May 22 "14 at 16:50
Thank you! There are so many ways khổng lồ get a value from an element with jQuery that it's inevitably a struggle khổng lồ find the way that you're looking for. – Charles Wood Jun 13 "14 at 14:59
Leo you can add a coalesc lớn get around the null issue e.g var selectedValues = $('#multipleSelect').val() || <>; Also worth noting it returns an array of strings. I was comparing khổng lồ an integer and getting no matches, so i added a .toString(). – tkerwood Jul 27 "16 at 2:05
You can also use js maps function:$("#multipleSelect :selected").map(function(i, el) ).get();
And then you can get any property of the option element:return $(el).text(); return $(el).data("mydata"); return $(el).prop("disabled"); etc...
great answer, but no need to lớn pay the extra expense of wrapping el as a jQuery object for every single option. Just go straight off the DOM when it's not too weird. You could change $(el).val() lớn just el.value. Of course if you're used to jQuery or want lớn grab data or attributes lượt thích your other examples, jQuery isn't hurting anyone. – KyleMit Aug 14 "15 at 20:42
KyleMit Great tip. Just used this approach to grab a collection of hidden field values and it worked perfectly. – EvilDr Jun 19 "17 at 10:32
best answer. thanks – John Cris Mañabo Mar 4 at 5:34
var selected=<>; $("#multipleSelect :selected").each(function()); console.log(selected);
Yet another approch to this problem. The selected array will have the indexes as the option values and the each array thành tựu will have sầu the text as its value.
Bạn đang xem: Jquery multiple select dropdown
if say option 1 and 2 are selected.
Xem thêm: gc invoker utility
the selected array will be :selected<"abc">=1; selected<"def">=2.
Just by one line-var select_button_text = $("#SelectQButton option:selected") .toArray().map(cống phẩm => thắng lợi.text);
Output: <"text1", "text2">var select_button_text = $("#SelectQButton option:selected") .toArray().map(công trình => thành tựu.value);
Output: <"value1", "value2">
If you use .join()var select_button_text = $("#SelectQButton option:selected") .toArray().map(chiến thắng => cửa nhà.text).join();
JQuery Code:$("#multiple :selected").each(function(i, sel));
Hope it works
Don't "hope it works", if you're not sure if it's the answer, kiểm tra it & be sure! – Sterling Archer Nov 20 "15 at 14:34
If you are not sure of the answer , then vì not post it..!! we are not here for hopes ..!! LOL – Clain Dsilva Dec 3 "15 at 13:16
Hey Man. It works perfectly. Cheông chồng it out. You should hope it. Don't give irrelevent comment.. – Prabhagaran Dec 17 "15 at 11:19
This is an inefficient usage of jQuery. Better is approach is khổng lồ preface with an ID selector like this: $('#multiple').find(':selected')
Prabhagaran – cannot_mutably_borrow Jan 18 "18 at 8:29
YounisShah I would hardly say it is "inefficient" as the time difference is relativity nothing... – NorCalKnockOut Feb 27 "18 at 22:36
Get selected values in comma separatorvar Accessids = ""; $(".multi_select .btn-group>ul>li input:checked").each(function(i,obj) ); Accessids = Accessids.substring(0,Accessids.length - 1); console.log(Accessids);
Thanks for contributing an answer to Stachồng Overflow!Please be sure to lớn answer the question. Provide details và chia sẻ your research!
But avoid …Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Post Your Answer Discard
Not the answer you're looking for? Browse other questions tagged jquery multi-select or ask your own question.
How vì you remove sầu all the options of a select box và then add one option và select it with jQuery?
Lagrange's equation is size invariant under ANY coordinate transformation. Hamilton's equations are not under ANY phase space transformation. Why? more hot questions
Stachồng Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Staông xã Exchange Network Technology Life / Arts Culture / Recreation Science Other
Staông xã Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters trò chơi Development
TeX - LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code Reviews
Magenkhổng lồ Software Recommendations Signal Processing Emacs Raspberry Pi Staông xã Overflow на русском Code Golf
Staông xã Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)
Blog Facebook Twitter LinkedIn Instagram
site kiến thiết / hình ảnh © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.15.38781