【问题标题】:event delegation problem with class类的事件委托问题
【发布时间】:2009-06-14 07:33:06
【问题描述】:

我想将jquery-form-validator 添加到我客户的旧 CMS。

这个 jqery 表单验证器需要插入一个类如 class="validate[required,custom[onlyLetter],length[0,100]]" 来输入。

<input class="validate[required,custom[onlyLetter],length[0,100]]" name="firstname" type="text" />


由于这是(从 90 年代开始的旧)CMS,我需要像这样通过 js 添加类。

var inputid = $(this).attr("id");

if(inputid=='navn'){
    $(this).removeClass().addClass('validate[required,custom[onlyLetter],length[0,100]] text-input');

}else if(inputid=='e-post'){
    $(this).removeClass().addClass('validate[required,custom[email]] text-input');
}else if (inputid=='telefon'){
    $(this).removeClass().addClass('validate[required,custom[telephone]] text-input');

它根据我的需要向输入标签添加类。但是,当我通过 js 添加类时,它不起作用。我认为这是因为事件委托。

谁能帮我在这种情况下怎么办?

提前致谢。

【问题讨论】:

    标签: jquery validation


    【解决方案1】:

    如果addClass 不起作用,可能是因为它的语法不正常……可能:

    $(this).attr('class','validate[required,custom[onlyLetter],length[0,100]]');
    

    可以吗?

    (编辑)

    验证器似乎也可能会读取一次值,而不是连续读取...也许确保在初始化验证库之前执行此操作?

    【讨论】:

    • 谢谢马克。正如您所指出的,我只需要在验证库之前添加类。
    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2011-03-29
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多