【问题标题】:Javascript radio button checked conditionJavascript单选按钮检查条件
【发布时间】:2021-09-03 02:51:42
【问题描述】:

我正在尝试在 Javascript 中运行一个条件,如果用户从 4 个单选按钮 ID 中选择一个,它会将表单提交按钮从禁用更改为启用。 我有以下代码,但我不确定出了什么问题:

 function validation(e) {
        var bt = document.getElementById('btSubmit');
        if (document.getElementById('1').checked && document.getElementById('1').checked && document.getElementById('2').checked && document.getElementById('4').checked && document.getElementById('5').checked) {
            bt.disabled = false;
        }
        else if (document.getElementById('3').checked) {
            document.getElementById("error").innerHTML = "Are you sure you do have not opinion?";
            document.getElementById("error").style.color = "green";
            document.getElementById("error").style.fontSize = "22px";
            document.getElementById("error").style.borderStyle = "solid";
        }
        else {
            e.preventDefault();
            document.getElementById("error").innerHTML = "You should fill this question";
            document.getElementById("error").style.color = "red";
            document.getElementById("error").style.fontSize = "30px";
            document.getElementById("error").style.borderStyle = "solid";
        }
    }
<form>
    <fieldset>
        <legend>Background Details</legend>
            <label for="age">Age:</label>
            <input type="text" id="age" name="age" placeholder="Enter your age"><br />
            <label for="age">Occupation:</label>
            <input type="text" id="occupation" name="occupation">
    </fieldset><br /><br />
    <h2>Select the best option:</h2>
    <label for="bars">To what extent did you enjoy the project?</label><br />
    <label for="bars">1 (Not at all)</label>
        <input type="radio" value="1" id="1">
        <input type="radio" value="2" id="2">
        <input type="radio" value="3" id="3">
        <input type="radio" value="4" id="4">
        <input type="radio" value="5" id="5">
    <label for="bars">5 (Very much)</label>
    <br /><br />
    <input type="button" value="Validate" onclick="validation()"><br />
    <input type="submit" value="Submit" id="btSubmit" disabled>
</form>
<p id="error"></p>

非常感谢您的帮助。 您的帮助总是帮助我学习和提高。

【问题讨论】:

    标签: javascript forms if-statement radio-button checked


    【解决方案1】:

    转换它

    <input type="button" value="Validate" onclick="validation()"><br />
    

    到这里

    <input type="button" value="Validate" onclick="validation(event)"><br />
    

    你必须将事件作为参数传递,否则函数不会接收到它

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2012-12-07
      • 2014-02-05
      • 1970-01-01
      • 2018-06-23
      • 2011-02-11
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      相关资源
      最近更新 更多