Js adds the default option value and text method under select

  • 2020-03-30 04:08:21
  • OfStack

< The pre name = "code" class = "Java" >

Drop-down box tag in JSP:

< S :select name=" SJX "id=" SJX" list="sjxList" listKey="BM" listValue="MC" size="20" cssStyle="width:100%; Height: 70 px; Border: 0 "multiple =" true "> < / s: select>


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

multiple="true" Multiple options are supported.
</pre><pre code_snippet_id="487056" snippet_file_name="blog_20141017_5_1612209" name="code" class="javascript">


Js flexible way to create the item under the select tag:


<pre name="code" class="javascript">var oSelect = $("sjx");<span style="white-space:pre"> </span>//SJX is the id of the select tag on an HTML or JSP page, and if you use Extjs, you can get the
tag with ext.getdom (' SJX ')
var oOption = document.createElement("OPTION");<span style="white-space:pre"> </span>//Create an OPTION subtab
under the select tag in js oSelect.options.add(oOption);<span style="white-space:pre"> </span>//Add the new OPTION child tag to
under the select tag oOption.value = "001";<span style="white-space:pre"> </span>//The value value of the content is
oOption.innerHTML =" The small apple ";<span style="white-space:pre"> </span>//Displays the contents of the drop-down box
... And so on

Note: js in this way, is more useful in certain situations, such as: here the request does not return a specific interface, that is, do not refresh the entire interface. Instead, asynchronous requests in the form of Ajax make some local data requests, and then the strut2 method below will not work.


<pre name="code" class="java"><pre name="code" class="java">for(...){
HashMap<String,Object> map = new HashMap<String,Objcet>();
map.put("BM","001");
map.put("MC"," The small apple ");
sjxList.add(map);
}

Another approach, which is also very common, is to define a List< in the Action using the struts2 feature. Object> The variable (in this case, named: sjxList) and sets the set and get methods.

Add content via a HashMap object, such as:

< / pre> When you return to the screen, "little apple" is displayed in the screen's select drop-down box.


<pre name="code" class="html"> The simplest way:  
 Directly in the jsp Page manual add select Of the label OPTION item  
<html> 
<body> 
<form> 
<select id="cars" name="cars"> 
<option value="volvo">Volvo</option> 
<option value="binli">Binli</option> 
<option value="mazda" selected="selected">Mazda</option> 
<option value="audi">Audi</option> 
</select> 
</form> 
</body> 
</html> 

Related articles: