简体   繁体   English

使用jQuery验证下拉列表

[英]Validate drop down with jquery

So I'm using Telerik's drop down list for a number of form inputs and I'm also validating that they're not empty with jquery client side. 因此,我将Telerik的下拉列表用于许多表单输入,并且还验证了jQuery客户端它们是否为空。 Here's what I have - it definitely validates the control but I can't get it to add a class to an object by looking for its parent. 这就是我所拥有的-它肯定可以验证控件,但是我无法通过查找其父级来将其添加到对象中。

Jquery: jQuery:

function validateCombo(source, args) {

    var combo = $find("<%= listWorkType.ClientID %>");
    var text = combo.get_text();
    if (text.length < 1) {        
        args.IsValid = false;
         combo.parents("div.selectWrap").addClass("error");
    }
    else {              
        args.IsValid = true;   
        combo.parents("div.selectWrap").removeClass("error");     
    }
}

.net 。净

<div class="selectWrap"> <rad:RadComboBox ID="listWorkType" Width="338" runat="server" InputCssClass="dropdown"></rad:RadComboBox></div>
<asp:CustomValidator ID="RFVDesiredJob" runat="server" Display="Dynamic" CssClass="formtext" ClientValidationFunction="validateCombo" ErrorMessage="Required" ></asp:CustomValidator>

As you can see from the jquery I'm wanting to add a class of 'error' to the div that is wrapping the drop down being validated. 正如您从jquery中看到的那样,我想向div中添加一类“错误”,以包装正在验证的下拉列表。 As stated the validation works perfectly but the div .selectWrap doesn't get the class added. 如前所述,验证工作正常,但是div .selectWrap没有添加类。

I can always just reference the div by class or create an ID but I'm doing this for a number of drop downs and I'm looking for a uniform solution. 我总是可以按类引用div或创建一个ID,但是我这样做是为了实现下拉菜单,我正在寻找一个统一的解决方案。

Any insight would be greatly appreciated. 任何见识将不胜感激。 Thanks. 谢谢。

Looks like you're trying to use jQuery on a non jQuery object, the $find fetches you the Telerik Client Side Object, so the combo.parents(..) throws an exception. 看起来您正在尝试在非jQuery对象上使用jQuery,$ find会获取您的Telerik客户端对象,因此combo.parents(..)会引发异常。 You could try something like this: 您可以尝试这样的事情:

 function validateCombo(source, args) {
        var combo = $find("<%= listWorkType.ClientID %>");
        //fetch the jQuery element as well
        var jqueryComboItem = $('#' + "<%= listWorkType.ClientID %>"); 
        var text = combo.get_text();
    if (text.length < 1) {        
        args.IsValid = false;
        jqueryComboItem.parents("div.selectWrap").addClass("error");
    }
    else {              
        args.IsValid = true;
        jqueryComboItem.parents("div.selectWrap").removeClass("error");     
    }
}

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

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