开发者

How to select option after page loads with prototype?

<select class="goog-te-combo">
  <option value="">select</option>
  <option value="ja">japan</option>
</select>

After page has loaded, I want to selec开发者_开发百科t the option whose value is "ja", i want to use prototype to do this automatic, anyone can help me ? thanks!


The answer seems obvious:

$$('option[value=ja]').first().selected = true;


Assuming you assigned an id to the <select>:

(function(element) {
    $A(element.options).each(function(option, index) {
       if ('ja' == option.value)
           element.selectedIndex = index;
    });
})( $('select-id') );

To retrieve all <select> elements of a given class, do:

$$('select.class_name_here').each(function(element) {
    $A(element.options).each(function(option, index) {
       if ('ja' == option.value)
           element.selectedIndex = index;
    });
});

Please refrain from using "denglish" in your code; it makes it look unsexy.

Here's a fiddle for that

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜