简体   繁体   English

基于从下拉菜单中选择的文本字段的 javascript 验证

[英]javascript validation of text field based on selection from drop down menu

Hi I have simple fix that is needed.嗨,我有需要的简单修复。 This validation is not working though all other validation works on the page.尽管页面上的所有其他验证都有效,但此验证无效。

   if ((document.getElementById("Authorizing_Company").value == "") && (document.getElementById("Authorizing_Title").selectedIndex == "Other Individual having knowledge of the affairs of the Corporation"))
     {
    alert(Authorizing_Company_msg);
    Authorizing_Company.select();
    Authorizing_Company.focus();
    return(false);              
    }

My HTML我的 HTML

<div class="col-md-6 mb-3">
        <label for="Authorizing_Title">Authorizing Person Position:</label>
      <select name="Authorizing_Title" id="Authorizing_Title" class="form-control form-elements firmnametoggle">
                    <option selected value="">--Select--</option>                         
                    <option value="Current Director">Current Director</option>                
                    <option value="Current Officer">Current Officer</option>                
                    <option value="Other Individual having knowledge of the affairs of the Corporation">Other Individual having knowledge of the affairs of the Corporation</option>                      
                 </select>
  </div>

<div class="col-md-6 mb-3" id="firmname">
        <label for="Authorizing_Company">*Name of Firm or Company:</label>
          <input name="Authorizing_Company" type="text" value="" class="form-control form-elements" id="Authorizing_Company" maxlength="255">
    </div>

This is a logic issue.The problem is on this line of code这是一个逻辑问题。问题出在这行代码上

(document.getElementById("Authorizing_Title").selectedIndex == "Other Individual having knowledge of the affairs of the Corporation")

In this line of code you try to compare the index of the option which is an integer to a line of string.在这行代码中,您尝试将整数选项的索引与一行字符串进行比较。 Therefore is will always be false .因此 is 永远是假的

To fix this you can either get the value of the option or just compare with the index.要解决此问题,您可以获取选项的值或仅与索引进行比较。

(document.getElementById("Authorizing_Title").selectedIndex == 3)

OR或者

(document.getElementById("Authorizing_Title").value== "Other Individual having knowledge of the affairs of the Corporation")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript-根据第一个字段中的选择在第二个字段上下拉显示 - javascript - drop down display on the second field based on selection in first field Rails根据下拉菜单选择填充文本区域 - Rails populate text area based on drop down menu selection 根据下拉菜单选择将对象属性插入文本字段 - Inserting object properties into text fields based on drop down menu selection 通过从 javascript 文件查询数据库,根据下拉菜单中的选择更新 div 内容 - Updating div content based on a selection from a drop down menu by querying a database from a javascript file 根据下拉选择动态生成文本字段和相应的标签 - dynamic generation of text field and the corresponding label based on the drop down selection 根据下拉菜单中的选择显示/隐藏表单字段 - Show/hide form fields based on a selection from a drop down menu 如何根据下拉菜单中的选择结果相乘 - How To Multiply Result Based on Selection from Drop Down Menu jQuery根据下拉菜单中的选择创建和删除字段 - jquery create and delete fields based on selection from a drop down menu 根据另一个菜单的选择更改第二个下拉菜单的值 - Changing the value of a second drop down menu based on selection from another javascript根据下拉选择自动填充文本输入 - javascript auto fill text inputs based on drop down selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM