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

在动态表上添加带有jquery的日历日期选择器

  •  1
  • Amra  · 技术社区  · 16 年前

    我正试图在创建时将jquery日历选择器添加到文本框中,但我找不到如何添加。

    当我按下按钮时,它将创建一个表,我要附加jquery日历选择器的元素是:

    var txtDate = createTextInput(i, "txtDate", 8, 10);
    txtDate.className  = "datepicker";
    this.newcells[i].appendChild(txtDate);
    

    我试着在最后加上:

    txtDate.datepicker();
    

    但不起作用。有人能帮忙吗?

    谢谢。

    注意:createtextinput是:

    function createTextInput(i, strName, size, maxLength) {
        var input = document.createElement('<input>');
        input.type = "text";
        input.name = strName;
        input.id = strName + i;
        input.size = size;
        input.maxLength = maxLength;
    
        return input;
    }
    
    3 回复  |  直到 16 年前
        1
  •  1
  •   rahul    16 年前

    使用

    $("#txtDate").datepicker();
    

    而不是

    txtDate.datepicker();
    

    您可以轻松地使用jquery创建一个元素。有点像

    $("<input type='text' />").attr("id", '').appendTo("yourelement");
    

    您可以在jquery中这样重写createtextinput函数

    function createTextInput(i, strName, size, maxLength) {
        return $("<input />".attr({
            type: 'text',
            name: strName,
            id: strName + i,
            size: size,
            maxLength: maxLength
        });    
    }
    

    如果返回jquery对象,则可以直接调用datepicker(),如

    txtdate.datepicker();
    
        2
  •  1
  •   Matti Virkkunen    16 年前

    如果 createTextInput 返回的是一个普通的dom节点而不是jquery对象,您需要使用 $() 关于结果。

    $(txtDate).datepicker();
    
        3
  •  1
  •   Alex    16 年前
    $(txtDate).datepicker();