2015-11-25 30 views
-1

我在表格行(tr)中有選擇字段和空閒輸入字段。 當我選擇一些選項的領域感覺與AJAX。我想要工作表中的ajax響應內部html tr

enter image description here

當我添加新行我想感覺輸入字段新行,而不是第一排。

enter image description here

它是表的html代碼:

<table class="table table-bordered" id="tbl1"> 
     <tr id="rowId"> 
      <td> 
       <select class="form-control" onchange="fetch_select(this.value)"> 
        <option></option> 
        <?php 
         $conn = mysqli_connect('localhost','root','','trailers'); 
         $query = "SELECT * FROM trailers"; 
         $run = mysqli_query($conn,$query); 

         while($row = mysqli_fetch_array($run)){ 
          $id = $row[0]; 
          echo '<option value="'.$id.'">'.$id.'</option>'; 
         } 
        ?> 
       </select> 
      </td> 
      <td><input type="text" name="text" class="form-control" placeholder="Movie Name" value="" readonly></td> 
     </tr> 
    </table> 

其我的Java腳本代碼

function fetch_select(val){ 
    $.ajax({ 
     type: 'post', 
     url: 'rs.php', 
     data: { 
      get_option:val 
     }, 
     success: function (response) { 
      document.getElementById("rowId").innerHTML=response; 
     } 
    }); 
} 

是添加/刪除Java腳本代碼:

$(document).ready(function(){ 
    var cnt = 2; 
    var a = '<?php $query = "SELECT * FROM trailers"; 
         $run = mysqli_query($conn,$query); 

         while($row = mysqli_fetch_array($run)){ 
          $id = $row[0]; 
          echo "<option>".$id."</option>"; 
         }?>'; 

    $("#anc_add").click(function(){ 


    $('#tbl1 tr').last().after('<tr><td></td><td><input type="text" name="text" class="form-control" placeholder="Movie Name" value="" readonly></td></tr>'); 
     $('#tbl1 tr:last-child td:first').append('<select class="form-control" onchange="fetch_select(this.value)"><option></'+a+'</select>'); 
    cnt++; 
    }); 

$("#anc_rem").click(function(){ 
    if($('#tbl1 tr').size()>1){ 
     $('#tbl1 tr:last-child').remove(); 
     }else{ 
     alert('One row should be present in table'); 
     } 
     }); 
}); 

這是我rs.php

<?php 

$conn = mysqli_connect('localhost','root','','trailers'); 

$req = $_POST['get_option']; 

if(isset($req)){ 

    $query = "SELECT * FROM trailers WHERE id = '$req'"; 
    $run = mysqli_query($conn,$query); 

    while($row = mysqli_fetch_array($run)){ 
     $nm = $row[1]; 

     echo '<tr>'; 
      echo '<td><select class="form-control" readonly></select></td>'; 
      echo '<td><input type="text" class="form-control" name="text" value="'.$nm.'"></td>'; 
     echo '</tr>'; 

     exit; 
    } 
} 
+0

一些HTML可以幫助給你一個解。 – ThinkTank

+0

你想要我的html嗎? –

回答