This article mainly describes how to dynamically add selectoption to JS & amp; JQuery. It is very helpful for reference. If you are interested, read it together. Today, a friend asked me about In the first place, the question of dynamically adding an option in Javascript was thought to be a dynamic addition in JS. Therefore, the method of dynamically adding an option in JS is used, but you are using JQuery, so it will keep making an error, the following is the difference between adding option in JS and JQuery.JS:Var selid = document. getElementById ("sltid"); for (var I = 0; I <10; I ++) {// Add multiple sid values cyclically. option [I] = new Option (I, I);} sid. options [sid. options. length] = new Option ("1", "2"); // Add one more after the last value JQuery:$ ("# SelectId"). append (""+ Text +""); Of course, in addition to this sentence, there are also set the default value, the first value, the last value, the N value, and so on, so I searched the internet:JQuery gets the selected Text and Value of Select:1. $ ("# select_id"). change (function () {// code...}); // Add an event for Select, triggered when one of the Select events is selected2. var checkText = $ ("# select_id"). find ("option: selected"). text (); // obtain the selected Text3. var checkValue = $ ("# select_id"). val (); // obtain the Value selected by Select4. var checkIndex = $ ("# select_id"). get (0). selectedIndex; // obtain the index value selected by Select5. var maxIndex = $ ("# select_id option: last"). attr ("index"); // obtain the largest index value of Select.JQuery adds/deletes Select Option items:1. $ ("# select_id"). append ("Text"); // Append an Option to Select (drop-down)2. $ ("# select_id"). prepend ("Select"); // Insert an Option for Select (first position)3. $ ("# select_id option: last"). remove (); // Delete the largest Option (last) index value in Select)4. $ ("# select_id option [index = '0']"). remove (); // Delete the Option with the index value 0 in Select (the first option)5. $ ("# select_id option [value = '3']"). remove (); // Delete the Option with Value = '3' In the Select statement5. $ ("# select_id option [text = '4']"). remove (); // Delete the Option of '4' Text = '4' in SelectThe above section describes how to dynamically add select option to JS and JQuery. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!For more articles about how to dynamically add select option in JS & JQuery, refer to the PHP Chinese website!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.