This code making a combo box.
public String getColor(String colorName)
{
mySB.append("<select onchange=\"ChangeColor(this);\" style=\"font-size:0.8em;\" id=\"").append(colorName).append("\" name=\"").append(colorName).append("\">")
.append("<option value=\"\"> </option>");
}
function ChangeColor(colors) {
var partcolor = (colors.options[colors.selectedIndex].value);
if (partcolor=="black"){
document.getElementById("colorRow").style.backgroundColor = 'black';
}
else if(partcolor=="brown") {
document.getElementById("colorRow").style.backgroundColor ='brown';
} else if(partcolor=="yellow") {
document.getElementById("colorRow").style.backgroundColor ='yellow';
}
}
I want to d开发者_运维百科ynamically update the color selection in a combo box using JavaScript. when the page reload, previous state of colors can be maintain in a Java app.
You mention page load, so one option would be to save the color selection in a query parameter, i.e. make the browser reload the same page in ChangeColor() with a URL that includes the color choice like this: http://...myurl...?color=purple, then examine the URL parameters during onLoad().
Depending on your design, you might be able to just dynamically update the color selection using JavaScript without a page reload?
Like this:
<div id="to_update_later"></div>
<script>
function ChangeColor(color) {
document.getElementById("to_update_later").innerText = "The color " + color;
}
</script>
This example shows how to replace the contents of a div tag dynamically from a JavaScript function. It is also possible to append instead of replacing. See for example http://www.w3schools.com/js/js_ex_dom.asp for tutorials on manipulating the HTML DOM objects of a web page.
精彩评论