源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript据option的value值快速设定初始的selected选项

  • 时间:2020-02-29 12:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript据option的value值快速设定初始的selected选项
<!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>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部