html表中的jquery datepicker,动态创建行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html表中的jquery datepicker,动态创建行相关的知识,希望对你有一定的参考价值。

我正在动态创建html表行,并打算在其中使用JQuery UIs Autocomplete和Datepicker。 The screen shot of webpage is like this

CSS和HTML代码是

<style>

            table {                
                font-family: arial, sans-serif;         
                border-collapse: collapse;          
                table-layout:fixed;         
                width: 100%;            
                padding: 2px;   
                border: 1px solid blue;                 
            }           
            th {            
                border: 1px solid blue;         
                padding-top: 10px;          
                padding-bottom: 10px;           
                background-color: #1b9add;          
                text-align: center;         

                valign: middle;         
                color: white;   
                background: #395870;
                background: linear-gradient(#49708f, #293f50);      
                max-width: 150px;           

            }           

            table td {          
                border: 1px solid blue;             
                word-wrap:break-word;           
                text-align: left;           
                vertical-align: top;            
                padding: 2px;           
            }           
            tr:nth-child(even) {            
                background-color: #dddddd;          
                padding: 2px;   
                border: 1px solid blue; 
            }           
            select option:checked {         
            color: white;           
            background: #f26532;            
            padding: 2px;           
            }           


            #buttonSet {
              position: fixed;
              bottom: 20px;
              right: 10px;
            }

        </style>    
        <title>Mail Management System: Register Send Mail</title>
        <div id="wrapper">

            <div class="container"> 
            <br>
                <div class="btn-group">
                  <button id="deletetblRow" class="btn btn-danger btn-sm">Delete Row</button>
                </div>

                  <div class="btn-group pull-right " >
                    <input class="your-input " type="number" style="width: 45px; height: 30px; font-weight: bold;" type="number" 
                            name="num_rows" id="num_rows" value="1" onfocus="this.select()" min="1" max="10" 
                            required=""/>
                    <button id="addtblRow" class="btn btn-primary btn-sm" >Add Row</button>
                </div>


                <table id="sendMailTable">
                  <thead>
                    <tr>
                      <th scope="col" style="width:  20px;">Sel</th>
                      <th scope="col" style="width:  75px;">Section</th>
                      <th scope="col" style="width: 175px;">File Reference</th>
                      <th scope="col" style="width: 100px;">Dated</th>
                      <th scope="col" style="width: 120px;">Security Grade</th>
                      <th scope="col" style="width: 125px;">Precedence</th>
                      <th scope="col" style="width: 100px;">Addressee</th>
                      <th scope="col" style="width:  100px;">Address</th>
                      <th scope="col" style="width: 140px;">Mode</th>
                <!--      <th scope="col">RL/Ticket No</th> -->
                    </tr>
                  </thead>
                    <tbody name='tblBody' id='tblBody'>
                    </tbody>
                </table>

            </div>

            <div class="container"> 
                    <div id="ajaxMsg" name="ajaxMsg" ><?php echo $SelectedCommandName; ?></div> 
            </div>

    <div id="buttonSet">
        <button id="btnSave" class="btn btn-primary">Save</button>
        <button id="btnCancel" class="btn btn-danger">Cancel</button>
    </div>
</div>

单击屏幕截图右上角的Add Row按钮即可填充数据行。 Dated(第三纵队)有DatepickerFile ReferenceAddress(最后一列)有Autocomplete。只要它只有一行,该程序就可以正常工作。在添加更多行时,自动完成和日期选择器不起作用。

添加新行的js代码如下

   function addNewRow(){
    var trd="";
    trd +="<tr>";
    trd +="<td><input type='checkbox' name='record' class='form-control'></td>";
    trd +="<td><select name='section' id='section' class='form-control'><?php echo $sectionNames ?></select></td>";
    trd +="<td><input name='fileRef' id='fileRef' class='form-control'></td>";
    trd +="<td><input class='form-control' type='text' placeholder='Mail Date'></td>";
    trd +="<td><?php echo $SecurityGrade ?></td>";
    trd +="<td><?php echo $Prece ?></td>";
    trd +="<td><?php echo $AddeeAppt ?></td>";
    trd +="<td><input name='address1' id='address1' class='form-control'></td>";
    trd +="<td><?php echo $ModesOfDes ?></td>";
    trd +="</tr>";
    $("table tbody").append(trd); 

}

在Stackoverflow上进行了大量的谷歌搜索和尝试了很多代码......但是徒劳无功。我可以在这里得到一些帮助。我相信这是最受追捧的查询。

答案

在DOM中创建新元素时,必须重新初始化jQuery工具自动完成和datepicker。

function addNewRow(){
    var trd="";
    trd +="<tr>";
    trd +="<td><input type='checkbox' name='record' class='form-control'></td>";
    trd +="<td><select name='section' id='section' class='form-control'><?php echo $sectionNames ?></select></td>";
    trd +="<td><input name='fileRef' id='fileRef' class='form-control'></td>";
    trd +="<td><input class='form-control' type='text' placeholder='Mail Date'></td>";
    trd +="<td><?php echo $SecurityGrade ?></td>";
    trd +="<td><?php echo $Prece ?></td>";
    trd +="<td><?php echo $AddeeAppt ?></td>";
    trd +="<td><input name='address1' id='address1' class='form-control'></td>";
    trd +="<td><?php echo $ModesOfDes ?></td>";
    trd +="</tr>";
    $("table tbody").append(trd); 
    // reinit datepicker and autocomplete here
}

我在这里找到了一个自动完成的例子:http://jsfiddle.net/r08m8vvy/4/

另一答案

我像这样调整了我的js代码,它运行顺畅

  <script>
  function addNewRow() {
  var trd = "";
  trd += "<tr>";
  trd += "<td><input type='checkbox' name='record' class='form-control'></td>";
  trd += "<td><select name='section' id='section' class='form-control'><?php echo $sectionNames ?></select></td>";
  //File Reference AutoComplete UI
  var appendFRTxt = "<td name='fr[]'><input class='form-control usedateFRautocomplete' name='fileRef' type='text' placeholder='File Ref' /></td>";
  trd += appendFRTxt; //These two steps through var necessary elese Throwing error
  //Mail Date
  var appendMDTxt = "<td name='md[]'><input class='form-control usedatepicker' name='mailDate' type='text' placeholder='Mail Date' /></td>";
  trd += appendMDTxt; //These two steps through var necessary elese Throwing error
  ///
  trd += "<td><?php echo $SecurityGrade ?></td>";
  trd += "<td><?php echo $Prece ?></td>";
  trd += "<td><?php echo $AddeeAppt ?></td>";
  //Stn List AutoComplete UI
  var appendSLTxt = "<td name='sl[]'><input class='form-control usedateSLautocomplete' name='stnlist' type='text' placeholder='Stn Address' /></td>";
  trd += appendSLTxt; //These two steps through var necessary elese Throwing error
  trd += "<td><?php echo $ModesOfDes ?></td>";
  trd += "</tr>";
  $("table tbody").append(trd);
  //File Ref
  $("tr").find("fr").append(appendFRTxt);
  var selectedSection = $('#section option:selected');
  var selsec = selectedSection.val();
  $.ajax({
    url: "../phpAssets/fileRefList.php",
    method: "POST",
    data: {
      selsec: selsec
    },
    success: function(data) {
      availableFileRefs = jQuery.parseJSON(new Array(data));
      $('.usedateFRautocomplete').autocomplete({
        source: availableFileRefs
      });
    }
  });
  //File Ref Ends
  //Mail Date   
  $("tr").find("md").append(appendMDTxt);
  $(".usedatepicker").datepicker({
    todayHighlight: true,
    format: 'dd M y',
    autoclose: true
  });
  //Mail Date Ends
  //Stn List
  $("tr").find("sl").append(appendFRTxt);
  var selectedSection = $('#section option:selected');
  var selsec = selectedSection.val();
  $.ajax({
    url: "../phpAssets/StnList.php",
    method: "POST",
    success: function(data) {
      availableStns = jQuery.parseJSON(new Array(data));
      $('.usedateSLautocomplete').autocomplete({
        source: availableStns
      });
    }
  });
  //Stn List Ends
}

addNewRow(); //Add One Row on loading the page 
$("#addtblRow").click(function() {
  var numofrows = $("#num_rows").val(); //numofrows is var desired by end-User
  frCounter++; //FileRef[] increment
  for (var i = 0; i < numofrows; i++) {
    addNewRow();
  }
});

// Find and remove selected table rows
$("#deletetblRow").click(function() {
      $("table tbody").find('input[name="record"]').each(function() {
        if ($(this).is(":checked")) {
          $(this).parents("tr").remove();
          frCounter--; //text box increment
        }
      });
<script>

I would like to say here that this improvement is based on guidance from this link

以上是关于html表中的jquery datepicker,动态创建行的主要内容,如果未能解决你的问题,请参考以下文章

MVC 项目中的 jQuery 错误:Datepicker 不是函数

jquery.datePicker日历控件中的一个小问题?????????

日期选择器:jquery datepicker的使用

以 HTML 形式更新 MySQL 行的 jQuery datepicker

如何在 MVC 4 中更改 jQuery DatePicker 中的验证文本?

jQuery UI 中的 datepicker( )方法