80分求助 如何改变默认的selected
<tr >
<td width= "472 " align=left> <b> Have children? </b> </td>
<td align=right id=s_h_t_match_have_children> <A HREF= "javascript:show_hide( 'match_have_children ') "> Hide </A> </td>
</tr>
<tr id=s_h_match_have_children>
<td colspan=2 align= 'left '>
<select class=main_text_l name= 'match_have_children ' onChange= "replace_text(); ">
<option value= '1 '> No </option>
<option value= '16 '> No, but I want children </option>
<option value= '32 '> No, and I don 't want children </option>
<option value= '0 '> No preference </option>
</select>
</td>
</tr>
JavaScript:
function show_hide(obj_name){
var obj = document.getElementById( 's_h_ '+obj_name);
var hideselect = document.getElementByName(obj_name);
if(obj.style.display== ' ') {
obj.style.display= 'none ';
document.getElementById( 's_h_t_ '+obj_name).innerHTML= " <A HREF=\ "javascript:show_hide( ' "+obj_name+ " ')\ "> Show </A> ";
for (var i=0; i < hideselect.obj_name.option.length; i++){
hideselect.obj_name.option[i].selected = false;
if (i == hideselect.obj_name.option.length - 1){
hideselect.obj_name.option[i].selected = true;
}
}
} else {
obj.style.display= ' '
document.getElementById( 's_h_t_ '+obj_name).innerHTML= " <A HREF=\ "javascript:show_hide( ' "+obj_name+ " ')\ "> Hide </A> ";
}
}
在网页打开的时候 默认selected的是 "No ",我想在点击hide后 默认的selected不在是 "No "而是 "No Preference ",请问JavaScript改怎么写呀 我上面自己写的不正确
------解决方案--------------------注:
不可用 getElementByName ,换成getElementById,原因不知.
修改焦点 用你现在用的这种方法可行,LS的第二种方法未通过.
第一种方法没有实验.(IE7下测试)
------解决方案--------------------function show_hide(obj_name){
var obj = document.getElementById( 's_h_ '+obj_name);
var hideselect = document.getElementsByName(obj_name)[0];
if(obj.style.display== ' ') {
obj.style.display= 'none ';
document.getElementById( 's_h_t_ '+obj_name).innerHTML= " <A HR