I am using Struts2 framework. I want to change the header value of a <s:select>
using javascript.
Here is my code:
<s:select label="Select Table" name="tableCheck" id="tableCheck" list="%{queryTableDisplay}" cssStyle="WIDTH:152px" headerKey="-1" headerValue="-- Select --"/>
Just get your select
element by id and change values in javascript.
// changes option value attribute
document.getElementById("tableCheck").options[0].value = "new_header_key";
// changes option inner HTML
document.getElementById("tableCheck").options[0].innerHTML = "new_header_value";
Even I have tried doing it and I have come to know that we cannot set header value of <s:select>
using javascript. On page-load,ie on DOM structure creation of page, <s:select>
expands and headerkey and header value form option-name parts of select tag.You can view it in page source. However, there is way by which you can set select list to some default value. To auto select a default value for a drop down box, just declared a “value” attribute in the tag, and set the default value accordingly.
JAVA FILE
public class SelectAction extends ActionSupport{
private List<String> searchEngine;
private String yourSearchEngine;
//set default value
public String getDefaultSearchEngine() {
return "yahoo.com";
}
public SelectAction(){
searchEngine = new ArrayList<String>();
searchEngine.add("google.com");
searchEngine.add("bing.com");
searchEngine.add("yahoo.com");
searchEngine.add("baidu.com");
}
//...
}
JSP
<s:select label="What's your favor search engine"
headerKey="-1" headerValue="Select Search Engines"
list="searchEngine"
name="yourSearchEngine"
value="defaultSearchEngine" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.