开发者

choose the item of a select

I need to select a specific item of a combobox in my page when i click in a button. I am using php and javascript code in my page.

Actually i am calling a javascript function at the "onclick" of the button. But i still dont get the right command to do this.

example:

<select id="TEST">
<option&g开发者_开发知识库t; a</option>
<option> b</option>
<option> c</option>
</select>

i want to show the item b when i click in the button.


<select id="select1">
    ...
</select>
<button onclick="chooseItem('select1', 1)">
<script type="text/javascript">
    function chooseItem(id, index){
        var selectElement = document.getElementById(id);
        selectElement.selectedIndex = index;
    }
</script>

or with jQuery:

<select id="select1">
    ...
</select>
<button id="button1">
<script type="text/javascript">
   $(document).ready(function(){
       $("#button1").click(function(e){
           $("#select1").each(function(){
               this.selectedIndex = 1;
           });
       });
   });

});


use selectedIndex property.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜