【问题标题】:JavaScript Validate Select ListJavaScript 验证选择列表
【发布时间】:2015-02-27 22:01:14
【问题描述】:

我花了几天的时间研究这个(包括查看这个网站),但我不明白该怎么做。

我正在尝试让 JavaScript 验证是否选择了默认空白选择以外的其他内容。

每次代码进入选择列表时,它都会弹出窗口警报,但不检查是否已选择任何内容。即使我选择了某些内容,也会弹出警报消息,然后继续执行代码,以便从未填写的字段中弹出其他错误警报。

我想让它知道是否选择了某些东西。如果它没有错误消息。如果没有选择任何内容,那么我希望只显示此警报消息。

这是与此列表有关的 Javascript 函数(在标题中)

function selectB() {
    x = 0;

    if (document.getElementById("mSQ").checked) {
        x++;
    }

    if (document.getElementById("pSQ").checked) {
        x++;
    }

    if (document.getElementById("cSQ").checked) {
        x++;
    }

    if (x == 0) {
        window.alert('You must select a Security Question');
        mSQ.focus();
        return false;
    }
}

这里是 HTML

    <p>Please Select a Security Question from the Drop Down List.<br />
    <select name = "Security Question">
        <option value = "d" id = "default" ></option>
        <option value = "m" id = "mSQ" >What is your Mother's maiden name?</option>
        <option value = "p" id = "pSQ" >What is the name of your pet?</option>
        <option value = "c" id = "cSQ" >What is your favorite color?</option>
    </select></p>

请帮忙,我在这个问题上卡了这么久。

【问题讨论】:

    标签: javascript forms validation select


    【解决方案1】:

    select 元素上的change 事件添加事件监听器。让该处理程序简单地检查select 元素的selectedIndex(> 0 => 某些东西被选中)。对 formsubmit 事件执行相同的操作,select 元素所在的位置。

    // assign handlers
    document.querySelector('#sq').addEventListener('change', check);
    document.querySelector('#testform').addEventListener('submit', check);
    
    function check(e) {
      var selector = document.querySelector('#sq');
      Helpers.logClear();
      Helpers.log2Screen('<b>', 
                         selector.selectedIndex > 0 
                           ? 'Selection ok' 
                           : 'Please <i>do</i> select an option', 
                         '</b> (from ', 
                         /form/i.test(this.nodeName) ? 'submit' : 'change', 
                         ')');
      return selector.selectedIndex > 0;
    }
    
    function submit() {
      Helpers.log2Screen('we are ok');
    }
    <script src="http://kooiinc.github.io/JSHelpers/Helpers-min.js"></script>
    <form id="testform">
      Please Select a Security Question from the Drop Down List.<br />
      <select id="sq" name = "Security_Question">
        <option value = "d"> </option>
        <option value = "m">What is your Mother's maiden name?</option>
        <option value = "p">What is the name of your pet?</option>
        <option value = "c">What is your favorite color?</option>
      </select>
      <input type="submit" value="submit" />
    </form>

    【讨论】:

    • 这只会在用户修改选择时运行。如果用户从不进入菜单,他仍然可以提交表单。
    • 有趣,我以前从未使用过“助手”。但是,我试图尽可能将代码编码为 JavaScript 的基础(如不使用外部链接来调用 JS)。感谢您抽出宝贵时间回答我的问题。
    【解决方案2】:

    .checked 用于复选框和单选按钮。它不用于选择选项。您使用.value 来获取所选选项的值。

    用途:

    var sec_question = document.getElementByName('Security Question')[0];
    if (sec_question.value == 'd') {
        window.alert('You must select a Security Question');
        sec_question.focus();
        return false;
    }
    

    您也可以使用 HTML5 required 属性,这样浏览器会自动执行检查。要使用它,默认选项应该有一个空值。

    <p>Please Select a Security Question from the Drop Down List.<br />
    <select name = "Security Question" required>
        <option value = "" id = "default" ></option>
        <option value = "m" id = "mSQ" >What is your Mother's maiden name?</option>
        <option value = "p" id = "pSQ" >What is the name of your pet?</option>
        <option value = "c" id = "cSQ" >What is your favorite color?</option>
    </select></p>
    

    【讨论】:

    • 感谢您的帮助。尽管我使用了下面的代码,但您解释了 select 的 .value 和 .checked 仅适用于单选框或复选框,这对我很有帮助。由于这些是我要处理的下一个领域,您的帖子仍然对我很有帮助。
    【解决方案3】:

    你真正要找的是看看 select 是否有值。

    这是您的代码的修改版本。

    <p>Please Select a Security Question from the Drop Down List.<br />
        <select name = "Security Question" id="securityQuestion">
            <option></option>
            <option value="m">What is your Mother's maiden name?</option>
            <option value="p">What is the name of your pet?</option>
            <option value="c">What is your favorite color?</option>
        </select>
        <input type="button" value="Click to Check" onClick="checkDropdown(1)" />
    </p>
    
    <script>
        function checkDropdown () {
        var securityQuestionElement = document.getElementById('securityQuestion');
        if(!securityQuestionElement.value) {  
            window.alert('You must select a Security Question');  
            securityQuestionElement.value = 'm'
            return false;  
        }
    }
    </script>

    主要区别

    • 我正在检查选择的值,而不是选项的“已检查”状态
    • 我正在选择byId,浏览器很早就索引了Ids(不确定按名称)
    • 我正在检查是否缺少值,而不是该值是否等于 3 种情况之一。 (更简洁/更易于阅读)

    【讨论】:

    • 非常感谢!这段代码确实有效,而且比我最初对 x 的想法更有效。
    猜你喜欢
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2013-10-18
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多