开发者

How I set the a field in a input select (HTML) with javascript?

开发者 https://www.devze.com 2023-03-05 02:42 出处:网络
I will rando开发者_JAVA百科mly select a number and define the selected field in a select with the sorted number. How I set the field?Add the Attribute selected=\"selected\" to the option element that

I will rando开发者_JAVA百科mly select a number and define the selected field in a select with the sorted number. How I set the field?


Add the Attribute selected="selected" to the option element that you want to select.

pseudo code

function foo()
{
create list of option elements
generate random number
get a single option element at index of random number
set all options attribute "selected" = null
set the option element[at index of random number] attribute selected = "selected"
}


var randomNumber = 3; // just for example 
document.getElementById('select_field').selectedIndex = randomNumber;
0

精彩评论

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