简体   繁体   English

无法使用Java语言添加类

[英]Unable to addClass using Javascript

I would like to style all the empty textboxes,by adding a class on validation (ie on Submit), but my code isn't working. 我想通过在验证时(即在Submit上)添加一个类来为所有空文本框设置样式,但我的代码无法正常工作。 Not sure if its the script or the customvalidator. 不知道它是脚本还是customvalidator。 I only want to use JS. 我只想使用JS。 If I say 如果我说

target.style.border="1px solid red";

its working though. 它的工作。 Just unable to add the class. 只是无法添加课程。

JS: JS:

<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
 function validateTextBox(sender, args) {
        var target = document.getElementById(sender.controltovalidate);
        var is_valid = target.value != "";
        if (is_valid) {
            target.removeClass("validate");
        }
        else {
            target.addClass("validate");
        }
        args.IsValid = is_valid;
    } 
</script>      

ASPX: ASPX:

 <asp:TextBox ID="txtSurname" runat="server"></asp:TextBox>
 <asp:CustomValidator ID = "CustomValidator1" runat="server" 
 ControlToValidate="txtSurname" ValidateEmptyText="true" 
 ClientValidationFunction="validateTextBox"
 ></asp:CustomValidator>

CSS: CSS:

 .validate
 {
   border:2px solid red;
 }

document.getElementById returns a DOM element, which does not have addClass or removeClass APIs. document.getElementById返回一个DOM元素,该元素没有addClass或removeClass API。

see: Element 请参阅: 元素

To apply class, try (putting the idea, please tweak according to exact business logic): 要申请课程,请尝试(提出想法,请根据确切的业务逻辑进行调整):

target.className = target.className + " validate"

If we go with raw JS, we have to write raw code and forget luxuries of addClass or removeClass :-) To remove class, see this answer: Remove class using javascript 如果我们使用原始JS,则必须编写原始代码,而忘了addClass或removeClass的奢侈品:-)要删除类,请参见以下答案: 使用javascript删除类

try this... 尝试这个...

function validateTextBox(sender, args) {
        var target = document.getElementById(sender.controltovalidate.ClientID);
        if (target.value != "") {
            target.removeClass("validate");
        }
        else {
            target.addClass("validate");
        }
        args.IsValid = "";
    }  

Try this. 尝试这个。 Simple way . 简单的方法。

function validateTextBox() {
        var target = document.getElementById("#<%= txtSurname.ClientID %>");
        **var is_valid = target.value;**
        **if (is_valid!="") {**
            target.removeClass("validate");
              **return true;**
        }
        else {
            target.addClass("validate");
            **return false**
        }

    } 

ASPX: ASPX:

 <asp:TextBox ID="txtSurname" runat="server"></asp:TextBox>
 <asp:CustomValidator ID = "CustomValidator1" runat="server" 
 ControlToValidate="txtSurname" ValidateEmptyText="true" 
 ClientValidationFunction=**"Javascript:return validateTextBox()"**
 ></asp:CustomValidator>

Maybe you would like to use jquery validation plugin ? 也许您想使用jquery验证插件 This is widely used plugin. 这是广泛使用的插件。

<asp:TextBox ID="txtSurname" runat="server" CssClass="toValidate"></asp:TextBox>

jQuery(function() {
    // You can specify some validation options here but not rules and messages
    jQuery('form').validate(); 
    // Add a custom class to your name mangled input and add rules like this
    jQuery('.toValidate').rules('add', { 
        required: true
    });
});

This code should check if textbox are empty before form submition. 此代码应在提交表单之前检查文本框是否为空。

change var target = document.getElementById(sender.controltovalidate.ClientID); 更改var target = document.getElementById(sender.controltovalidate.ClientID);

to: var target = document.getElementById(sender.ClientID); 到: var target = document.getElementById(sender.ClientID);

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

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