简体   繁体   中英

Dynamic Jquery Validation of Dropdownlist

I'm attempting to dynamically add jquery validation to some dynamically created items in my asp.net webforms project (asp:checkbox, asp:textbox, infragistics:webnumericeditor, asp:dropdownlist) and can get it working correctly for all items except the dropdownlist.

The code I'm using is:

var oID = this.ObjectID;
$("#" + oID).rules("add", {
    required: {
        depends: function (element) {
            return $("#" + oID + " option:selected").val() == 0;
        }
     },
     messages: {
         required: "*"
     }
 })

The dropdownlist item is generated from codebehind with a 'Please Select...' item inserted at position 0 like so

DropDownList ddl = new DropDownList();
...
ddl.DataBind();
ListItem li = new ListItem("Please Select...", "0");
ddl.Items.Insert(0, li);

The validation is never firing though. I added an alert to fire when the item gets generated and that returns 0 correctly so I'm unsure why the validation isn't working.

This is my first time using jquery validation so I'm expecting it to be something obvious and simple but the methods I've tried thus far haven't yielded any results so any help is much appreciated!

ListItem li = new ListItem("Please Select...", " ");

为您的默认项目指定''而不是指定为'0'的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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