2012-10-13 44 views
0

我想创建一个带有文本框的div,我希望datepicker显示日期。到目前为止,我能够动态地创建带有文本框和日期选择器的div,但我无法在文本框上显示日期。我在这里做错了什么?在动态创建的文本框上显示日期选择器

代码:

showdate(){ 
var newDateBoxdiv = $(document.createElement('div')).attr({ class:"EDate", id:"EDate"+i}); 
newDateBoxdiv.html('<input type="text" id="DisplayTextBox">'); 

addDateBox(); 


newDateBoxdiv.insertAfter('.EDate'); 
} 



//Function called inside the above function 
addDateBox(){ 
$('.EDate').find('input[id^="DisplayTextBox"]').each(function() { 

    $(this).datepicker({ 
     showOn: "button", 
     buttonImage: "calendar.png", 
     buttonImageOnly: true 

    }); 
    $(this).datepicker({dateFormat: 'dd-M-y'}); 
    var dateFormat = $(this).datepicker("option", "dateFormat"); 
    $(this).datepicker("option", "dateFormat", 'dd-M-y'); 

}); 

回答

1

更新代码..香港专业教育学院注意到你的代码的一些错误,其中包括newDateBoxdiv值。您的文本框DisplayTextBox也不会增加。试试这个。

showdate(){ 
    var edatenum = $('#increment').val(); 
    edateinc = "EDate"+edatenum; //increment the value of your div 
    var displaytextbox = "DisplayTextBox"+edatenum; //of course you have to increment your textbox also 
    $('#increment').val(""+(edatenum+1)); //increment the value of our hidden field 
    var newDateBoxdiv = $('<div class="EDate" id="'+edateinc+'"> <input type="text" id="'+displaytextbox+'"></div>'); 
    newDateBoxdiv.insertAfter(".your_existing_box"); 

    addDateBox(); //im not sure if this method already works? 
} 

<!-- outside of script --> 
<input type='hidden' id='increment' value='1'> 
+0

http://jsfiddle.net/nick_craver/5Dm7f/1/ 检查此小提琴。但是我试图在网络上运行相同的代码,但它没有响应。没有任何工作。 –

+0

它对我的工作?当我在每个日期选择器中选择一个日期日期显示到字段中?有什么问题? – loQ

+0

也适合我。但是当我尝试在我的项目中工作这个代码它没有响应。我是否需要包含js文件?如果是的话呢? –

1

使用。对():

$(选择).live(事件,数据,处理程序); // jQuery 1.3+

$(document).delegate(selector,events,data,handler); // jQuery 1.4.3+

$(document).on(events,selector,data,handler); // jQuery 1.7+

相关问题