最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ui datepicker not working with dynamic html - Stack Overflow

programmeradmin12浏览0评论

I have successfully attached jquery UI datePicker to a dynamically created textbox, Al works fine excepts when I select a date it is not ing in corresponding textbox, But it es in my first textbox which is not dynamically created. My code is like this

 $(".add-more").click(function () {
        $this = $(this);
        $section = $($("." + $this.attr("data-section"))[0]).html();// This is a section in my HTML with multiple textboxes
        $this.parent().append('<div class=' + $this.attr("data-section") + '>' + $section + '</div>').fadeIn(); 

        $(".datepicker").removeClass('hasDatepicker').datepicker();

    });

HTML

<div id="Div1" class="section-container">
    <div class="education-item" style="display: none">
        <p>Institute:<input type="text" name="txtInstitute"   /></p>
        <p>Start Year:<input type="text" name="txtStartYear" class="datepicker" /></p>
        <p>End Year:<input type="text" name="txtEndYear"  class="datepicker"/></p>
    </div>
</div>

JSFiddle Here

I have successfully attached jquery UI datePicker to a dynamically created textbox, Al works fine excepts when I select a date it is not ing in corresponding textbox, But it es in my first textbox which is not dynamically created. My code is like this

 $(".add-more").click(function () {
        $this = $(this);
        $section = $($("." + $this.attr("data-section"))[0]).html();// This is a section in my HTML with multiple textboxes
        $this.parent().append('<div class=' + $this.attr("data-section") + '>' + $section + '</div>').fadeIn(); 

        $(".datepicker").removeClass('hasDatepicker').datepicker();

    });

HTML

<div id="Div1" class="section-container">
    <div class="education-item" style="display: none">
        <p>Institute:<input type="text" name="txtInstitute"   /></p>
        <p>Start Year:<input type="text" name="txtStartYear" class="datepicker" /></p>
        <p>End Year:<input type="text" name="txtEndYear"  class="datepicker"/></p>
    </div>
</div>

JSFiddle Here

Share Improve this question asked Mar 15, 2014 at 10:48 NoneNone 5,68023 gold badges93 silver badges178 bronze badges 2
  • Try jsfiddle/xUYM2 ( i just use more accurate indication of elements to create datepicker ) – Vasiliy vvscode Vanchuk Commented Mar 15, 2014 at 11:33
  • Ahhh..That work like a charm. You can add this as answer here :) – None Commented Mar 15, 2014 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 5

Try to use more accurate indication of elements to create datepicker. Like here http://jsfiddle/xUYM2/

$(".add-more").click(function() {
    $this = $(this);
    $section = $($("." + $this.attr("data-section"))[0]).html();
    var block = $('<div class=' + $this.attr("data-section") + '>' + $section + '</div>')
    $this.parent().append(block).fadeIn();

    block.find(".datepicker").removeClass('hasDatepicker').datepicker();
    showHideRemove($(this).parents(".section-container").find(".remove-item"), 1);
});

P.S> it will be better to use local variables in you script like

    var $this = $(this); // without var keyword your variable will be global
    var $section = $($("." + $this.attr("data-section"))[0]).html();

Add this line in the add-more function

$(".datepicker").removeClass('hasDatepicker').datepicker(); 
发布评论

评论列表(0)

  1. 暂无评论