一个select框中有四个值,如何在点击button时将select框的第三个值选中?试用js脚本编写 50
3个回答
展开全部
可用如下代码实现,但要注意,元素索引从0开始,所以第三个选项的索引值是2
<select id="slt">
<option value="123">123</option>
<option value="abc">abc</option>
<option value="xyz">xyz</option>
<option value="ttt">ttt</option>
</select>
<input type="button" onclick="javascript:document.getElementById('slt').getElementsByTagName('option')[2].selected=true;" value="按钮">
<select id="slt">
<option value="123">123</option>
<option value="abc">abc</option>
<option value="xyz">xyz</option>
<option value="ttt">ttt</option>
</select>
<input type="button" onclick="javascript:document.getElementById('slt').getElementsByTagName('option')[2].selected=true;" value="按钮">
展开全部
试试吧!
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.onload = function(){
document.getElementById("btn").onclick = function(){
var sel = document.getElementById("sel");
sel.selectedIndex = 2;
}
}
//-->
</SCRIPT>
<body>
<select id="sel">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<button id="btn">Click</button>
</body>
</html>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.onload = function(){
document.getElementById("btn").onclick = function(){
var sel = document.getElementById("sel");
sel.selectedIndex = 2;
}
}
//-->
</SCRIPT>
<body>
<select id="sel">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<button id="btn">Click</button>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Document.getElementById('select id').value='第三项的值'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询