怎样在页面加载时,给html select 指定值

hellunter 2007-11-06 11:22:34
我的页面有一个html select控件,我怎样在页面加载时,指定select某项 checked
下面是我的代码,但总有错,getElementById("Select1")找不到对象。
我该怎样做,请大虾们指点
body onload= "initA() ">
<script type= "text/javascript " language= "JavaScript ">
function initA()
{
var areaChecked= document.getElementById( 'Select1 ');
areaChecked.value=100;
alert( "test ");
}
</script>

<select id= "Select1 " name= "sltArea ">
<option selected= "selected " value= "90 "> 全省 </option>
<option value= "100 "> 中心 </option>
</select>
...全文
563 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hellunter 2007-11-11
  • 打赏
  • 举报
回复
是不是在 onLoad 中无法实现?
路人乙e 2007-11-07
  • 打赏
  • 举报
回复
var area = location.href.substr(location.href.indexOf("area=")+5)
hellunter 2007-11-07
  • 打赏
  • 举报
回复
To lishijie910123
document.getElementById( "Select1 ").value=传过来的值;
写在onload事件中,

会报错,我想可能加载时,还未生成Select1 ,所以document.getElementById( "Select1 "),找不到对象
写在onload中有些问题
lishijie910123 2007-11-07
  • 打赏
  • 举报
回复
document.getElementById("Select1").value=传过来的值;
写在onload事件中
就可以了
活靶子哥哥 2007-11-06
  • 打赏
  • 举报
回复
try

<head>
<script type="text/javascript">
function initA()
{
var areaChecked= document.getElementById('Select1');
areaChecked.selectedIndex = 1;
}
</script> </head>
<body onload= "initA()">



<select id= "Select1" name= "sltArea ">
<option selected= "selected " value= "90 "> 全省 </option>
<option value= "100 "> 中心 </option>
</select>
hellunter 2007-11-06
  • 打赏
  • 举报
回复
谢谢各位,我的问题需要更进一步的解决,我预先不知哪个选项checked,根据页面 request["area"]来决定,但是把代码放于body onload又不起作用,大家有没有好的思路?
路人乙e 2007-11-06
  • 打赏
  • 举报
回复
<select id= "Select1 " name= "sltArea ">
<option selected= "selected " value= "90 "> 全省 </option>
<option value= "100 "> 中心 </option>
</select>
<script>document.Forms[0].sltArea.selectedIndex=1;</script>
qq22345111 2007-11-06
  • 打赏
  • 举报
回复
var choose=document.getElementById("ProtectPassword1");//Select控件
if(choose.options[choose.selectedIndex].text=="请选择")//其中某一个被选中,执行代码
{

document.getElementById("ProtectPassword2").style.display="none";
document.getElementById("CheckProtect").style.visibility="visible";
return false;
}
kings2015 2007-11-06
  • 打赏
  • 举报
回复
帮顶

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧