[英]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.