【问题标题】:datePicker onSelect function not retrieving classes added before from it-selfdatePicker onSelect 函数不从自身检索之前添加的类
【发布时间】:2015-06-17 10:47:04
【问题描述】:

这是我的代码

 onSelect: function (dateText, inst)
 {
    inst.inline = false;
    $(".ui-datepicker-current-day").addClass("markupDate");
 }     

当我检查 html 代码时,“markupDate”类已成功添加到 html 元素中。

我选择另一个日期,如果日期已经有一个“markupDate”类,我用我的 onSelect 函数检查。如果没有,我添加它。

问题是,当我点击一个已经有“markupDate”类的日期并用jquery询问单元格是否有“markupDate”类时,结果为假。

但是当检查 chrome 中的元素时,$(".ui-datepicker-current-day") 已经有类 "markupDate" !!

这里是代码

    onSelect: function (dateText, inst)
 {
     //no markupDate class in the result !
    console.log(($(".ui-datepicker-current-day").attr("class")));
    inst.inline = false;
     /*result is ever false !*/
    if($(".ui-datepicker-current-day").hasClass("markupDate")){
    //do something
    }else{
    $(".ui-datepicker-current-day").addClass("markupDate");
  }
 }  

jquery-ui库中_selectDay的代码是

    _selectDay: function (a, b, c, d) {
        var e = $(a);
        if ($(d).hasClass(this._unselectableClass) 
  ||  this._isDisabledDatepicker(e[0])) 
   return;
   /*i put my code here and i return  =>my code is working */
     var f = this._getInst(e[0]);
      f.selectedDay = f.currentDay = $("a",d).html(), f.selectedMonth = 
     f.currentMonth = b, f.selectedYear =f.currentYear = c, 
    this._selectDate(a, this._formatDate(f, f.currentDay, f.currentMonth, 
    f.currentYear))

当我直接在里面添加我的代码时,我在这一行之前返回:

          var f = this._getInst(e[0]);

onSelect 函数检索 $(".ui-datepicker-current-day") 是否具有类 "markupDate"。

感谢您的帮助。

【问题讨论】:

    标签: javascript jquery jquery-ui datepicker jquery-multidatespicker


    【解决方案1】:

    乍一看,您似乎在使用 hasClass 而不是 hasClass。

    改变这个

     if($(".ui-datepicker-current-day").haveClass("markupDate")){
    

    到

     if($(".ui-datepicker-current-day").hasClass("markupDate")){
    

    【讨论】:

    • 是的,我会编辑我的帖子,谢谢。在我使用 hasClass 的产品中,这不是问题。
    • 另外,您不必检查。已有类时调用 addClass 不会添加两次。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多