簡體   English   中英

如何使用on()通過Jquery向動態添加的div中添加日期選擇器

[英]How to add a datepicker to a dynamically added div with Jquery with on()

我將div添加到正在創建的表單中。 我想使用on()函數來執行此操作,但似乎無法使其正常工作。 據我了解,您必須使用一個事件,但是有一種方法可以使div中的事件成為現實。 我一直在嘗試的代碼帶有“ click”事件,但這並不是我想要的。 第一次單擊后將添加日期選擇器,但根本沒有添加buttonImage。 我需要它才能在第一次點擊時起作用。 這是我現在正在使用的代碼:

  $('#switcher-panel').on('click', 'table tr td input', function(){
    $('.datepicker').datepicker({
      buttonImage: "images/calendar.png"
    });
  });

這是我正在使用的html:

<table class="table table-hover" data-controller="rank">
  <thead>
    <tr>
      <th colspan="4" align="left"><h2> Boy Scout Rank</h2></th>
    </tr>
    <tr>
      <th>Number</th>
      <th>Requirements</th>
      <th>Completed</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <tr data-name="one_li">
      <td>1</td>
      <td>
        Req
      </td>
      <td>
        <input class="datepicker"/>
      </td>
    </tr>
  </tbody>
</table>

我不明白該如何處理。 任何幫助都會很棒。

從上面的代碼來看,您似乎沒有添加新的<div />

如果不是這樣,則無需添加事件(點擊)處理程序-http://jsfiddle.net/ZNR7P/

您應該能夠:

$(function() {

    $('.datepicker').datepicker();

});​
$(function(){

    $('.datepicker').datepicker({
        showOn:"button", // or "both" if you want the input to trigger the datepicker too
        buttonImage: "images/calendar.png"
    });

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM