Method _ javascript tips for adding the default option value and text under select in js

Source: Internet
Author: User
This article mainly introduces how to create html tags in js and add the default option value and text under select. It is very practical and can be viewed by friends.

The drop-down box label in jsp:

The Code is as follows:


 

Multiple = "true" means multiple options are supported.



How to flexibly create items under the select label in js:

The Code is as follows:


Var oSelect = $ ("sjx"); // sjx is the id of the select tag on the html or jsp page. If Extjs is used, EXT. getDom ('sjx') to obtain tags

Var oOption = document. createElement ("OPTION"); // create the OPTION sub-tag under the select tag in js
OSelect. options. add (oOption); // add the newly created OPTION sub-tag to the select tag.
OOption. value = "001"; // value corresponding to the content
OOption. innerHTML = "apple"; // the content of the displayed drop-down box
... And so on

Note: This method in js is useful in specific scenarios. For example, the request does not return a specific interface, that is, the whole interface is not refreshed. Instead, asynchronous requests using Ajax are used to make some local data requests. In this case, the following strut2 method will be invalid.

The Code is as follows:


For (...){
HashMap Map = new HashMap ();
Map. put ("BM", "001 ");
Map. put ("MC", "apple ");
SjxList. add (map );
}

Another method is also very commonly used: using the features of struts2, define a List in ActionVariable (in this example, name it sjxList) and set the set and get methods.

Add content through a HashMap object, for example:

When the interface is returned, the "apple" is displayed in the select drop-down box ".

The simplest method is to manually add the OPTION item of the select label on the jsp page.     

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.