2012-03-16 57 views
0

我想根據選定的選項禁用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> 

它不起作用。它出什麼問題了?

回答

1

嗯,首先,你有一個額外的「}」其次,你可能想要點擊事件,而不是模糊事件。而且你希望它在兩個單選按鈕上。接下來什麼是document.f?這不是一個變數。接下來,即使它是,我不知道一個瀏覽器,可以讓你使用表單元素,就像你正在嘗試。例如,document.f.radio2[0].disabled。另外,您的單選按鈕應該有唯一的ID名稱。

參見:http://jsfiddle.net/vzYT3/1/更明智的事情。

2

你的代碼中有太多「}」(最後一個)。

請勿使用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,

--hennson

0

你可以提高你的編碼一點,看看這個例子:

<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/

它不會是一個壞主意,使用JavaScript像jQuery庫。

+0

您的示例使用Mootools – yossico 2014-09-01 06:30:52