SoFunction
Updated on 2025-02-28

js method to add the default option value and text under select

<pre name="code" class="java">

Pull-down box tags in jsp:

<s:select name="sjx" list="sjxList" listKey="BM" listValue="MC" size="20" cssStyle="width:100%;height:70px; border:0" multiple="true"></s:select>

Copy the codeThe code is as follows:

<pre name="code" class="html">

multiple="true" means to support multiple selections.
</pre><pre code_snippet_ snippet_file_name="blog_20141017_5_1612209" name="code" class="javascript">


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

Copy the codeThe code is as follows:

<pre name="code" class="javascript">var oSelect = $("sjx");<span style="white-space:pre"> </span>//sjx is the id of the select tag on the html or jsp page. If you use Extjs, you can use ('sjx') to get the tag

var oOption = ("OPTION");<span style="white-space:pre"> </span>// create OPTION subtitles under the select tag in js
(oOption);<span style="white-space:pre"> </span>//Add the newly created OPTION sub-label to the select tag
= "001";<span style="white-space:pre"> </span>//The value corresponding to the content
="Little Apple";<span style="white-space:pre"> </span>//The content of the drop-down box displayed
...and so on

Note: This method in js is more useful in specific occasions, such as: the request here does not return a specific interface, that is, it does not refresh the entire interface. Instead, use Ajax method to make some local data requests, then the following strut2 method will be invalid.

Copy the codeThe code is as follows:

<pre name="code" class="java"><pre name="code" class="java">for(...){
HashMap<String,Object> map = new HashMap<String,Objcet>();
("BM","001");
("MC","Little Apple");
(map);
}

Another method is also very commonly used: use the feature of struts2 to define a List<Object> variable in Action (taking this example as an example, named: sjxList), and set the set and get methods.

Add content through a HashMap object, such as:

</pre>When returning to the interface, "Little Apple" will be displayed in the select drop-down box of the interface.

&lt;pre name="code" class="html"&gt;The easiest way: 
Directly injspAdd the page manuallyselectTagsOPTIONitem 
&lt;html&gt; 
&lt;body&gt; 
&lt;form&gt; 
&lt;select  name="cars"&gt; 
&lt;option value="volvo"&gt;Volvo&lt;/option&gt; 
&lt;option value="binli"&gt;Binli&lt;/option&gt; 
&lt;option value="mazda" selected="selected"&gt;Mazda&lt;/option&gt; 
&lt;option value="audi"&gt;Audi&lt;/option&gt; 
&lt;/select&gt; 
&lt;/form&gt; 
&lt;/body&gt; 
&lt;/html&gt;