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.
精彩评论