开发者

choose the item of a select

开发者 https://www.devze.com 2022-12-15 17:46 出处:网络
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.

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

精彩评论

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

关注公众号