开发者

How to change Selected Value attribute/property on runtime?

开发者 https://www.devze.com 2023-02-28 21:26 出处:网络
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 co

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消