チェックボックスとラジオボタンの読み取り専用状態を実現するためのJavaScriptを多用する方法を考え出しました。Firefox、Opera、Safari、Google Chromeの現在のバージョン、およびIEの現在および以前のバージョン(IE7まで)に対してテストされています。
あなたが尋ねる無効なプロパティを単純に使用してみませんか?ページを印刷すると、無効になっている入力要素が灰色で表示されます。これが実装された顧客は、すべての要素が同じ色になることを望んでいました。
会社で働いていたときに開発したので、ここにソースコードを投稿できるかどうかはわかりませんが、確かにコンセプトは共有できます。
オンマウスダウンイベントを使用すると、クリックアクションによって選択状態が変更される前に、選択状態を読み取ることができます。したがって、この情報を保存してから、onclickイベントを使用してこれらの状態を復元します。
<input id="r1" type="radio" name="group1" value="r1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
<input id="r2" type="radio" name="group1" value="r2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
<input id="r3" type="radio" name="group1" value="r3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 3</input>
<input id="c1" type="checkbox" name="group2" value="c1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
<input id="c2" type="checkbox" name="group2" value="c2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
<input id="c3" type="checkbox" name="group2" value="c3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 3</input>
この場合、JavaScriptの部分は次のように機能します(ここでも概念のみ)。
var selectionStore = new Object(); // keep the currently selected items' ids in a store
function storeSelectedRadiosForThisGroup(elementName) {
// get all the elements for this group
var radioOrSelectGroup = document.getElementsByName(elementName);
// iterate over the group to find the selected values and store the selected ids in the selectionStore
// ((radioOrSelectGroup[i].checked == true) tells you that)
// remember checkbox groups can have multiple checked items, so you you might need an array for the ids
...
}
function setSelectedStateForEachElementOfThisGroup(elementName) {
// iterate over the group and set the elements checked property to true/false, depending on whether their id is in the selectionStore
...
// make sure you return false here
return false;
}
入力要素のonclickおよびonmousedownプロパティを変更することにより、ラジオボタン/チェックボックスを有効/無効にできるようになりました。