2012-03-16 67 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

相关问题