代码之家  ›  专栏  ›  技术社区  ›  sipsorcery

jquery提取ID,然后基于类附加日历

  •  0
  • sipsorcery  · 技术社区  · 14 年前

    我正在使用一个现有的JavaScript库,它执行Ajax调用,然后执行一个可选的回调函数。回调函数将传递正在加载到DOM中的HTML字符串。我需要将jquery日期选择器分配给该HTML字符串中属于日历类的所有元素。

    Ajax查询中HTML字符串的一个简化示例是:

    <tr id='1234'>
        <td>
            <input type="text" value="somevalue" />
            <input type="text" class="calendar" value="21/12/2010" />
        </td>
        <td>
            <input type="text" value="someothervalue" />
            <input type="text" class="calendar" value="21/12/2011" />
        </td>
    </tr>
    

    我可以在回调函数中执行以下操作,将datepicker设置为两个必需的输入,但这会导致datepicker发生一些奇怪的事情,例如从2010年跳到1900年的下一个按钮和跳到1899年的上一个按钮。

    $(".calendar").datepicker('destroy').datepicker({ dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', duration: 200, gotoCurrent: true, changeYear: true, yearRange: 'c-120:c' });
    

    因此,我只想将语句限制在提供给回调函数的HTML片段中的元素。我可以使用什么jquery模式来实现以下结果,注意html参数是一个字符串:

    function SetCalendar(html) {
        $("html.children .calendar").datepicker('destroy').datepicker({ dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', duration: 200, gotoCurrent: true, changeYear: true, yearRange: 'c-120:c' });
    

    }

    2 回复  |  直到 14 年前
        1
  •  1
  •   redsquare    14 年前

    你差点就受够了。如果将它用作Ajax的成功函数。

    function ajaxSuccessFn(html) {
        $(html).find('input.calendar').datepicker({
              dateFormat: 'dd/mm/yy',
              showAnim: 'fadeIn',
              duration: 200,
              gotoCurrent: true,
              changeYear: true,
              yearRange: 'c-120:c' 
        }).end().appendTo('#someElement');
    }
    
        2
  •  0
  •   sipsorcery    14 年前

    也许有更好的方法,但这是可行的。我仍然有一个恼人的问题,即日期选取器上的下一个按钮从2010年7月跳到1900年1月,但我猜这是日期选取器代码中的其他内容。

    function SetCalendar(html) {
        var id = "#" + /.*?id="(.*?)"/.exec(html)[1];
        $(id).find('input.calendar').datepicker('destroy').datepicker({ 
              dateFormat: 'dd/mm/yy', 
              showAnim: 'fadeIn', 
              duration: 200, 
              gotoCurrent: true, 
              changeYear: true, 
              yearRange: 'c-120:c'  
        }); 
    }