简体   繁体   English

没有document.ready函数的jqueryUI日期选择器

[英]jqueryUI datepicker without document.ready function

To create jquery datepicker, we use the following function 为了创建jquery datepicker,我们使用以下函数

 $( function() {
    $( "#datepicker" ).datepicker();
  } );
<input type="text" id="datepicker">

I am trying to achieve a in-line editing functionality with a new record function as below 我正在尝试通过以下新记录功能实现内联编辑功能

function createRowForAdd(){
    var tRow = "<tr>"
    for(var i = 0; i < array.length; i++){
        var jsonObj = array[i];
        tRow +="<td><input type='text' id='"+jsonObj.id+"'  /></td>" 
    }
    tRow += "</tr>";
    return tRow;
}

    function Add(){
      var tRow  = createRowForAdd();
      $("#tblId tbody").append(tRow); 
}

<button id="btnAdd" onclick="javascript:Add()">New</button>  
<table id="tblId" border="1">   
        <thead> 
            <tr> 
                <th>Name</th> 
                <th>Birth Date</th> 
                <th>Joining Date</th> 
                <th></th> 
            </tr>
        </thead>
    <tbody>
    </tbody>
    </table> 

One or more columns may contain a date field. 一列或多列可能包含日期字段。 For those column(s), I would like to show a date picker. 对于这些列,我想显示一个日期选择器。 As I understand, document.ready function gets triggered once the DOM is ready. 据我了解,DOM准备就绪后将触发document.ready函数。 Is it possible to initiate a date picker on row add? 是否可以在行添加上启动日期选择器?

.datepicker() function just needs that the element is in the DOM, so you can execute it after you add the row without problems, applying your desired selector for that input fields. .datepicker()函数只需要该元素位于DOM中,因此您可以在添加行而不会出现问题之后执行该元素,并为该输入字段应用所需的选择器。

About the selector, considering that you're going to have multiple datapicker inputs, avoid using the same id (id's are designed to be unique in the DOM). 关于选择器,考虑到您将有多个数据选择器输入,请避免使用相同的id (ID被设计为在DOM中是唯一的)。 You better use a class instead. 您最好改用一个类。

function Add() {
    var tRow  = createRowForAdd();
    $("#tblId tbody").append(tRow);

    // When creating the row, set class="datepicker" the inputs of the row that
    // has to be converted to a datetime picker. Then you just have to do this...
    $("input.datepicker").datepicker();
}

Or even better, apply it only to the inputs from the new added row (the last one)... 甚至更好的是,仅将其应用于新添加的行(最后一行)的输入...

function Add() {
    $("#tblId tbody").append(createRowForAdd()).find('tr:last input.datepicker').datepicker();
}

EDITED : I can't see the value of your array variable, but looking at your code it looks like all the inputs of the same column will have the same id . 编辑 :我看不到您的array变量的值,但查看您的代码,看起来同一列的所有输入将具有相同的id As I mention earlier, avoid that because id's are designed to be unique in the DOM. 如前所述,请避免这种情况,因为id's被设计为在DOM中是唯一的。 If you need an id, you can use the row number to change the input for every id. 如果需要一个ID,则可以使用行号来更改每个ID的输入。 Here you have an example with that idea... 这里有一个关于这个想法的例子...

HTML : HTML

<button id="btnAdd">New</button>  
<table id="tblId" border="1">   
  <thead> 
    <tr> 
      <th>Name</th> 
      <th>Birth Date</th> 
      <th>Joining Date</th> 
      <th></th> 
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

JQUERY : JQUERY

// I'm making up the content of this array. The question doesn't show it.
var array = [
    { 'id': 'name', 'class': '' },
    { 'id': 'birthdate', 'class': 'datepicker' },
    { 'id': 'joiningdate', 'class': 'datepicker' },
    { 'id': 'something', 'class': '' }
];

function createRowForAdd(rowPos) {
    var tRow = [];
    for (var i=0, l=array.length; i<l; i++)
        tRow.push('<td><input type="text" id="'+array[i].id+rowPos+'" class="'+array[i].class+'" /></td>'); 
    return '<tr>' + tRow.join('') + '</tr>';
}

$('button#btnAdd').click(function() {
    var rowPos = $("table#tblId tbody tr").length;
    $("table#tblId tbody").append(createRowForAdd(rowPos)).find('tr:last input.datepicker').datepicker();
});

And the fiddle... https://fiddle.jshell.net/rigobauer/tpxnvpy4/ 还有小提琴... https://fiddle.jshell.net/rigobauer/tpxnvpy4/

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM