js 將checkbox, radio, select控件設置爲只讀不可選



1 <script language=javascript>
function dis()
{
var a = document.getElementsByTagName_r("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_r("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.getElementByIdx_x(radioId);
radioObj.checked = !radioObj.checked;
var radioObjGroup = document.forms[0].elements(radioName);
var groupIdx = document.getElementByIdx_x(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.getElementByIdx_x("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')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章