2016-05-17 58 views
0

下面我試圖通過單擊按鈕將datepicker元素設置爲輸入字段。由於所有日期選擇器都具有相同的類名,因此單擊按鈕會將其全部更改爲輸入字段。但是我想只改變那個按鈕那一行中的那個datapicker。通過點擊動態加載的表中同一行的另一個字段來更新一行的字段

HTML

<!DOCTYPE html> 
    <html> 
     <head> 
     <!--Import Google Icon Font--> 
     <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 
     <!--Import materialize.css--> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css"> 
    <link rel="stylesheet" href="custom.css"> 
     <!--Let browser know website is optimized for mobile--> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
     </head> 

     <body> 

    <div> 
    <div class="row"> 
    <div class="input-field col s6 offset-s2"> 
     <input id="contact" type="text" class="validate"> 
     <label for="Contact">Enter contact name.</label> 
    </div> 

    <div class="md col s4"> 
    <a class="bt waves-effect waves-light btn">Search</a> 
    </div> 
    </div> 

    <div class="table-margin"> 
    <table id="mytable"> 
      <thead> 
      <tr> 
       <th data-field="id">Contact Name</th> 
       <th data-field="phone">Phone</th> 
       <th data-field="Data"> Data</th> 
       <th data-field="Action"> Action</th> 
      </tr> 
      </thead> 

      <tbody> 


      </tbody> 
     </table> 
     </div> 



     <!--Import jQuery before materialize.js--> 
     <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script> 
     <script src="script.js"></script> 
     <script src="new.js"></script> 
     </body> 
    </html> 

JS1

 $(document).ready(function() { 


      var status = [ 
         { 
         "id": 1, 
         "name": "sourabh", 
         "phone": "811880", 
         "email":"[email protected]" 
         }, 
         { 
         "id": 6, 
         "name": "sourabh", 
         "phone": "255888888", 
         "email": "[email protected]" 
         }, 
         { 
         "id": 6, 
         "name": "sourabh", 
         "phone": "255888888", 
         "email": "[email protected]" 
         }, 
         { 
         "id": 6, 
         "name": "sourabh", 
         "phone": "255888888", 
         "email": "[email protected]" 
         }]; 

     var len = status.length; 
     var x= '<input type="date" class="dt datepicker">'; 
     var y= '<button class="make waves-effect waves-light btn" type="button">Update</button>'; 
     data = ""; 
      if(len > 0){ 

      for (var i = 0; i < len; i++){ 
       data = data + "<tr><td>"+status[i].name+"</td><td>"+status[i].phone+"</td><td>"+x+"</td><td>"+y+"</td><tr>"; 
      } 

     } 

     $("#mytable tbody").append(data) 


     }); 

JS2。

 $(document).ready(function() { 

    $('.datepicker').pickadate({ 
     selectMonths: true, // Creates a dropdown to control month 
     selectYears: 15 // Creates a dropdown of 15 years to control year 
     }); 


    $('.make').click(function(){ 
     var x = this.rowIndex; 
     console.log(x); 
     $('.dt').replaceWith($('<input type="text" value="Input box">')); 

    });}); 

回答

1

使用$(this).closest("tr").find(".dt"),而不是$(".dt")所以只選擇了同一行,你點擊的元素在日期選擇器。

+0

非常感謝。它運作良好。 – scripter

相關問題