[英]Disable radio button according to selected choice
我想根據選擇的選項禁用html表單中的某些單選按鈕,如果他在第一個單選按鈕組中選擇了第一個選項,則第二個單選按鈕組中的2個選項將被啟用,如果沒有,則將其禁用,這是我的碼:
<script language="javascript">
function RadioMeuble() {
if (document.f.radio1[0].checked) {
alert("Vous avez choisi la proposition " + document.f.radio1[0].value);
document.f.radio2[0].disabled = false;
document.f.radio2[1].disabled = false;
} else {
document.f.radio2[0].disabled = true;
document.f.radio2[1].disabled = true;
}
}
}
</script>
<input type="radio" name="radio1" value="L" id="radio1" onBlur="RadioMeuble()">á louer</label>
<br>
<label>
<input type="radio" name="radio1" value="V" id="radio1">á vendre</label>
</p>
<p>Superficie
<label for="textfield"></label>
<input type="text" name="superficie" id="Superficie">en Km ²</p>
<p>Prix
<label for="textfield2"></label>
<input type="text" name="prix" id="Prix">en DT</p>
<p>Meublé
<input type="radio" name="radio2" id="radio2" value="oui" disabled>Oui
<input type="radio" name="radio2" id="radio2" value="non" disabled>
<label for="radio2"></label>
<label for="radio"></label>Non</p>
沒用 它出什么問題了?
您的代碼中有一個“}”太多(最后一個)。
不要使用onblur EventHandler。 您應該改用onchange或onclick。
<input type="radio" name="radio1" value="L" id="radio1" onchange="RadioMeuble()">
要么
<input type="radio" name="radio1" value="L" id="radio1" onclick="RadioMeuble()">
HTH,
-亨森
好吧,首先,您要有一個額外的“}”;其次,您可能希望單擊事件而不是模糊事件。 而且您希望在兩個單選按鈕上都使用它。 接下來是什么document.f? 那不是一個變量。 接下來,即使是這樣,我也不知道有一個瀏覽器可讓您像嘗試使用的那樣使用表單元素。 例如, document.f.radio2[0].disabled
。 另外,您的單選按鈕應具有唯一的ID名稱。
請參閱: http : //jsfiddle.net/vzYT3/1/,以獲取更明智的信息。
您可以改善編碼,請檢查以下示例:
<input type="radio" id="r1-1" name="r1" value="1">
<label for="r1-1">Option 1</label>
<input type="radio" id="r1-2" name="r1" value="2">
<label for="r1-2">Option 2</label>
<hr />
<input type="radio" id="r2-1" name="r2" value="a">
<label for="r2-1">Option A</label>
<input type="radio" id="r2-2" name="r2" value="b">
<label for="r2-2">Option B</label>
和
function byId(id) {
return document.getElementById(id);
}
window.onload = function() {
byId('r1-1').onchange = byId('r1-2').onchange = function() {
byId('r2-1').disabled = byId('r2-2').disabled = byId('r1-1').checked;
}
}
在以下位置運行示例: http : //jsfiddle.net/5Mp9m/
使用像Jquery這樣的javascript庫並不是一個壞主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.