Javascript 下拉链接
我想选择带有链接的组合的索引。Javascript 下拉链接,javascript,select,load,selectedindex,Javascript,Select,Load,Selectedindex,我想选择带有链接的组合的索引。 我试过这个: <script> eval(document.location.hash.substring(1)); function poke(num, zat) { var selObj = document.getElementById('okey'); selObj.selectedIndex = num - 1; var selObj = document.getElement
我试过这个:
<script>
eval(document.location.hash.substring(1));
function poke(num, zat) {
var selObj = document.getElementById('okey');
selObj.selectedIndex = num - 1;
var selObj = document.getElementById('nope');
selObj.selectedIndex = zat - 1;
}
</script>
<select id="okey">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<select id="nope">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<button onclick="eval(document.location.hash.substring(1));">okey</button>
eval(document.location.hash.substring(1));
功能戳(num,zat){
var selObj=document.getElementById('okey');
selObj.selectedIndex=num-1;
var selObj=document.getElementById('nope');
selObj.selectedIndex=zat-1;
}
一
二
三
四
五
一
二
三
四
五
好吧
按钮工作正常,但当我尝试加载页面时,它显示错误
错误是这样的:
未捕获类型错误:无法将属性“selectedIndex”设置为null
我该怎么办?代码是一样的。但负载不工作,按钮工作。嗯?没有selectedIndex属性。您可以通过设置
selected
选项元素上的属性哦,我找到了现在,我的代码是这样的:
<body onload="eval(document.location.hash.substring(1));">
<script>
function poke(num, zat) {
var selObj = document.getElementById('okey');
selObj.selectedIndex = num - 1;
var selObj = document.getElementById('nope');
selObj.selectedIndex = zat - 1;
}
</script>
<select id="okey">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<select id="nope">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
功能戳(num,zat){
var selObj=document.getElementById('okey');
selObj.selectedIndex=num-1;
var selObj=document.getElementById('nope');
selObj.selectedIndex=zat-1;
}
一
二
三
四
五
一
二
三
四
五
谢谢所有想帮助我的人!:) 为什么要在“onclick”处理程序中评估代码?因为,我想确定代码是否正常工作。:)我不确定如何通过评估代码来实现这一点:)我找到了它,谢谢你的帮助:)当然有一个
selectedIndex
–它已经在DOM级别1中定义,请参阅并查看它,它应该会对您有所帮助。