【问题标题】:Disable radio button according to selected choice根据所选选项禁用单选按钮
【发布时间】:2012-04-02 03:05:29
【问题描述】:

我想根据所选选项禁用 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>

它不起作用。它有什么问题?

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您的代码中有太多“}”(最后一个)。

    不要使用 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,

    --汉森

    【讨论】:

      【解决方案2】:

      嗯,首先,你有一个额外的“}”其次,你可能想要点击事件而不是模糊事件。你想要它在两个单选按钮上。接下来什么是document.f?那不是变量。接下来,即使是这样,我也不知道有一个浏览器可以让您像尝试那样使用表单元素。例如,document.f.radio2[0].disabled。此外,您的单选按钮应该有唯一的 ID 名称。

      请参阅:http://jsfiddle.net/vzYT3/1/ 以获得更明智的信息。

      【讨论】:

        【解决方案3】:

        你可以稍微改进一下你的编码,看看这个例子:

        <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 库也不错。

        【讨论】:

        • 您的示例使用 Mootools
        猜你喜欢
        • 2018-01-27
        • 1970-01-01
        • 2011-11-19
        • 2017-06-30
        • 1970-01-01
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多