javascript据option的value值快速设定初始的selected选项
所属分类:
网络编程 / JavaScript
阅读数:
810
收藏 0赞 0分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>written by misshjn</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(value){
var opt = document.getElementById("ss").innerHTML.toString();
var re = new RegExp(value,"g");
opt = opt.replace(/<OPTION|<\/OPTION>| selected|value/g,"").match(/=.*?>/g).toString().replace(/=|>/g,"").replace(re,"┢").replace(/[^,┢]/g,"").indexOf("┢");
document.getElementById("ss").getElementsByTagName("option")[opt].selected = true;
}
function randomselect(){
document.getElementById("ss").getElementsByTagName("option")[Math.floor(Math.random()*document.getElementById("ss").getElementsByTagName("option").length)].selected = true;
}
//-->
</SCRIPT>
</head>
<body>
<!--
<select id="ss">
<option value="1998">1998年</option>
<option value="1999">1999年</option>
<option value="2000">2000年</option>
<option value="2001">2001年</option>
<option value="2002">2002年</option>
<option value="2003">2003年</option>
<option value="2004">2004年</option>
<option value="2005">2005年</option>
</select>
-->
<SCRIPT LANGUAGE="JavaScript">
<!--
var opts;
for (i=1900; i<2008; i++){
opts +="<option value='"+i+"'>第 "+i+" 年</option>";
}
document.write("<select id='ss'>"+opts+"</select>");
//-->
</SCRIPT>
<input type="button" value="选中 value=2001 的选项" onclick="test('2001')">
<input type="button" value="随机选择" onclick="randomselect()">
</body>
</html>
Vue-Cli项目优化操作的实现
这篇文章主要介绍了Vue-Cli项目优化操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
vue 全局环境切换问题
小编在开发使经常会碰到全局切换问题,今天小编给大家带来一篇教程给大家介绍vue 全局环境切换问题,感兴趣的朋友一起看看吧
收藏 0赞 0分享
element-ui 本地化使用教程详解
这篇文章主要介绍了element-ui 本地化使用教程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
在Vue项目中,防止页面被缩放和放大示例
今天小编就为大家分享一篇在Vue项目中,防止页面被缩放和放大示例,具有很好的参考 价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0赞 0分享
vue h5移动端禁止缩放代码
今天小编就为大家分享一篇vue h5移动端禁止缩放代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0赞 0分享
Vue 3.0双向绑定原理的实现方法
这篇文章主要为大家详细介绍了Vue 3.0双向绑定原理的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0赞 0分享
查看更多