2010-04-13 65 views
1

我想添加一個Jquery日曆選取器在創建時的文本框,但我無法找到如何。添加日曆datepicker與動態表上的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; 
} 
+0

謝謝到@Matti,@亞歷山大和@Rahul,但你的答案目前還沒有解決我的問題。 我不知道我在哪裏放置那條線它不是正確的地方。 有人可以告訴我在哪裏放$(txtDate).datepicker(); ? 我在this.newcells [i] .appendChild(txtDate)之後添加它。 謝謝。 – Amra 2010-04-13 10:36:47

+0

如果txtDate是你的文本框的ID,那麼你必須這樣寫代碼:$(「#txtDate」)。datepicker();' – rahul 2010-04-13 11:16:38

回答

1

使用

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

,而不是

txtDate.datepicker(); 

您可以創建使用jQuery輕鬆的元素。喜歡的東西

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

您可以重寫createtextinput功能像這樣的jQuery的

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

如果要返回jQuery對象,那麼你可以直接調用日期選擇器(),像

txtDate.datepicker(); 
+0

會選擇名稱還是id ?,因爲元素有一個id和一個不同於id的名字。謝謝。 – Amra 2010-04-13 10:07:09

+0

它是一個id選擇器。 – rahul 2010-04-13 10:21:46

+0

如果你想選擇使用名稱,那麼你可以寫'$(「input [name = inputname]」) – rahul 2010-04-13 10:22:22

1

如果createTextInput返回一個普通的DOM節點,而不是一個jQuery對象,你需要在結果使用$()

$(txtDate).datepicker(); 
+0

這不起作用,我沒有得到任何錯誤,但沒有添加它。 有沒有其他信息可以幫助你來幫助我? 謝謝。 – Amra 2010-04-13 10:05:59