日期:2014-05-16  浏览次数:20307 次

js 将checkbox, radio, select控件设置为只读不可选(转)

1 <script?? language=javascript>??
function?? dis()??
{??
??????? var?? a?? =?? document.getElementsByTagName("input");??
??????? for?? (var?? i=0;?? i<a.length;?? i++)??
??????? {??
????????????? if?? (a[i].type=="checkbox"?? ||?? a[i].type=="radio")?? a[i].disabled=true;??
??????? }??
??????? var?? b?? =?? document.getElementsByTagName("select");??
??????? for?? (var?? i=0;?? i<b.length;?? i++)??
??????? {??
????????????? b[i].disabled=true;??
??????? }??
}??
</script>??
<input?? type=button?? value="失效"?? onclick=dis()>

注:失效的checkbox,radio等提交后得到的是空值,不管是否选择了

2 <select???? onchange="selectedIndex=2">??
<option?? value=1>1</option>??
<option?? value=2>2</option>??
<option?? value=3?? selected>3</option>??
<option?? value=4>4</option>??
</select>??
<input?? type=checkbox?? checked?? onclick="checked=defaultChecked">??
<input?? type=radio?? onclick="checked=defaultChecked">

22<input?? type="radio" name="ss" checked onclick="checked=defaultChecked">
<input?? type="radio" name="ss" onclick="document.forms[0].ss[0].checked=true">
<input?? type="radio" name="ss" onclick="document.forms[0].ss[0].checked=true">
???


3 如何将JSP中RadioBox设为不可选状态,并且不变灰
关键字: radiobox不可选并且不变灰
试过了onchange,onclick事件都不好使。(不用 disabled="true"的情况下)
比如:onclick="JavaScrirpt: return false;"
结果是确实不可以重新选中一组RadioBox中的其它 单选钮,但是页面加载时的初始选中的 单选钮 会被置为 未选状态(只要你点击了其它radiobox)。
onchange="JavaScript:if(this.checked) {this.checked = false;} else{this.checked = true;}" 也有问题,不好解决。
而加一种方法是用 disabled="true" 后再更改 radiobox的CSS样式。
可结果我发现只能改变 背景 没办法改变 前景色(即radiobox始终是灰的。)

这种问题当然我要是用大段的逻辑代码来控制当然是可以实现的,但我不愿那样做,太没必要了。
请高手们指点呀。
//******************************************************************************************
<script language="javascript">
function resumeRadio(radioId, radioName, hidId) {
var radioObj = document.getElementById(radioId);
radioObj.checked = !radioObj.checked;
var radioObjGroup = document.forms[0].elements(radioName);
var groupIdx = document.getElementById(hidId).value;
if (groupIdx.length > 0) {
radioObjGroup[parseInt(groupIdx)].checked = true;
}
}
</script>

<input type="hidden" name="familyTypeIndex" id="familyTypeIndex" value="" />
<logic:iterate id="familytyps" name="FamilyForm" property="diagnosisUnitOptions" indexId="index">
<bean:define id="codeValue">
<bean:write name="familytyps" property="code" />
</bean:define>
<logic:equal name='FamilyForm' property='familyBasics.familyType' value='<%= (String)pageContext.getAttribute("codeValue") %>'>
<script language="JavaScript">
document.getElementById("familyTypeIndex").value = "<%= index %>";
</script>
</logic:equal>
<html:radio property='familyBasics.familyType' styleId='radioFamilyType<%= index %>' value='<%= (String)pageContext.getAttribute("codeValue") %>' onclick='resumeRadio(this.id,this.name,"familyTypeIndex")'/>
<bean:write name="familytyps" property="codeName" />
</logic:iterate>

都弄成这样了,还是不行。
老说这里有语法错误,我真的搞不懂了
onclick='resumeRadio(this.id,this.name,"familyTypeIndex")'
我把this.id,this.name换成相应的 字符串形式也不行。
//******************************************************************************************
奶奶地,终于好了。
注意这句的写法
<html:radio property='familyBasics.familyType' value='<%= (String)pageContext.getAttribute("codeValue") %>' onclick="resumeRadio(this.id,this.name,'familyTypeIndex')" styleId='<%= "radioFamilyType" + index %>' />
这里面的'familyTypeIndex'一定要用单引号包围。
resumeRadio(this.id,this.name,'familyTypeIndex')