Pau*_*ulo 0 html javascript php
当我点击一个按钮时,我需要在页面中选择一个组合框的特定项目.我在我的页面中使用php和javascript代码.
实际上我在按钮的"onclick"上调用了一个javascript函数.但我仍然没有得到正确的命令来做到这一点.
例:
<select id="TEST">
<option> a</option>
<option> b</option>
<option> c</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我想在单击按钮时显示项目b.
<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>
Run Code Online (Sandbox Code Playgroud)
或者使用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;
});
});
});
Run Code Online (Sandbox Code Playgroud)
});