简体   繁体   English

使用JavaScript进行下拉验证

[英]Dropdown Validation using javascript

I am doing a validation that checks whether a value other than Please Select... is selected in a asp.net drop down button with the use of javascript. 我正在做一个验证,检查是否使用JavaScript在asp.net下拉按钮中选择了Please Please ...以外的值。 I am firing the validation function in the onchange of the drop down event and if the value is Please Select..., I am displaying a message in the label control. 我在下拉事件的onchange中触发验证功能,并且如果值是“请选择...”,则在标签控件中显示一条消息。

Below is my aspx code. 下面是我的aspx代码。

  document.getElementById("ContentPlaceHolder1_drpEditAlerts").onchange = function validate_Quest() { var edAlertSelect = document.getElementById("ContentPlaceHolder1_drpEditAlerts"); if (edAlertSelect.selectedIndex == 0) { document.getElementById("lblEdtAlert").innerHTML = 'Kindly select an alert!'; document.getElementById('lblEdtAlert').style.color = "red"; document.getElementById("ContentPlaceHolder1_Button3").disabled = true; return false; } document.getElementById("lblEdtAlert ").innerHTML = ''; document.getElementById("ContentPlaceHolder1_Button3").disabled = false; return true; } 
 <p> Select an alert to link this scenario* <span style="float: right"> <asp:Label ID="lblEdtAlert" Text="" ForeColor="Red" Font-Size="Smaller"></asp:Label> </span> <asp:DropDownList ID="drpEditAlerts" runat="server" Height="32px" Width="500px" /> </p> 

The code is working fine and when I have the please select value in dropdown its showing me the message in the label. 代码工作正常,当我在下拉菜单中选择值时,它会在标签中显示消息。 However when I select a value in the drop down that label message is still showing the error and is not getting closed. 但是,当我在下拉列表中选择一个值时,该标签消息仍显示错误且未关闭。

Any help is highly appreciated. 非常感谢您的帮助。

Simpler 更简单

 var select = document.getElementById("ContentPlaceHolder1_drpEditAlerts"); var label = document.getElementById("lblEdtAlert"); var button = document.getElementById("ContentPlaceHolder1_Button3"); window.onload = function() { select.onchange = function() { var ok = this.selectedIndex > 0; label.innerHTML = ok ? "" : 'Kindly select an alert!'; label.style.color = ok ? "black" : "red"; button.disabled = !ok; } } 
 <p> Select an alert to link this scenario* <span> <label id="lblEdtAlert"></label> </span> <select id="ContentPlaceHolder1_drpEditAlerts"> <option>Please Select</option> <option>Other 1</option> <option>Other 2</option> </select> <button id="ContentPlaceHolder1_Button3">send</button> </p> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM