How to change Selected Value attribute/property on runtime?
Here is the aspx/page file g开发者_StackOverflow社区enerated:
screenshot
The controls were created by parsing XML using XSLT. Is it possible to change the selected values by running some lines of code using ASP.NET or just plain ASP, or maybe even javascript being called from code-behind?
If you use jQuery (which you should consider because it's awesome) you could do that by doing the following
<script type="text/javascript">
$(function)({
$('select[name="sel_Option"]').val('1');
});
</script>
In JavaScript, once you have the reference to the select
element (either via getElementById
or by getElementsByName
) you could use
function setSelected(selectBox, valueToSelect){
selectBox.value = valueToSelect;
}
See http://jsfiddle.net/nogoodatcoding/DSqXe/
You could also use the selectedIndex property:
function setSelected(selectBox, value){
for(var i = 0; i < selectBox.length; i++){
if(selectBox.options[i].value === value){
selectBox.selectedIndex = i;
}
}
}
See http://jsfiddle.net/nogoodatcoding/DSqXe/1/
精彩评论