0

My aim is dynamically setting the option tag's default display.
I want to create a function,and give it two variables as parameters.Like this function(para1, para2) and the first parameter is the option tag,the second is a value.If the first parameter's value or id equals the second value,this option tag will be set to default display.
I want to know how to write the first expression in brackets.I have used document.getElementsByTagName(option) and others,but they are all wrong.

4

1 に答える 1

0

Though I am not sure exactly what you are looking, this is my best guess with jQuery solution
Assuming
para1 - id of the dropdown
para2 - value which should be selected in dropdwon

You can use .val

$("#tagId").val(desiredValue);

in your case it will be like

$("#para1").val(para2);

In javascript

var index;
var dd = document.getElementById('para1');
for (var i = 0; i < dd.options.length; i++) {
    if (dd.options[i].text === para1) {
          index =i;        
          break;
    }
}
dd.options[index].selected = true;
于 2012-12-21T05:44:13.647 に答える