2016-07-22 42 views
0

我无法访问我在php标签内创建的文本框值到ajax调用。当我在ajax中打印文本框的值时,会调用它的'undefined',那么是否有任何方式来访问文本框的值。看到在代码中的注释在管路39如何访问php标签内创建的文本框值到ajax调用

<html> 
 
<?php 
 
include 'config.php'; 
 
include 'dbconnect.php'; 
 
?> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
 
    <table border="1"> 
 
     <tr> 
 
      <th>categoryId</th> 
 
      <th>category</th> 
 
      <th>Operations</th> 
 
     </tr> 
 
     <?php 
 
     $sql_query = "select category_id,category from cart_category_descriptions limit 10;"; 
 
     $result = $conn->query($sql_query); 
 
     if ($result->num_rows > 0) { 
 
      while($row = $result->fetch_assoc()) { 
 
       echo '<tr id='.$row["category_id"].'><td>'.$row["category_id"].'</td><td><input type=text name="cat" value='.$row["category"].'></td><td><button class="deletedata">Delete</button><button class="updatedata">Update</button></td></tr>'; 
 
      } 
 
     } 
 
     else { 
 
      echo "0 results"; 
 
     } 
 
     ?> 
 
     <script> 
 
     $(document).on('click','.deletedata',function(){ 
 
      id = $(this).closest('tr').attr('id'); // Get the clicked id for deletion 
 
      alert(id); 
 
      $.ajax({ 
 
       type:'POST', 
 
       url:'delete.php', 
 
       data:{delete_id:id}, 
 
       success:function(data){ 
 
         window.location.reload(); 
 
       } 
 
      })}); 
 
     $(document).on('click','.updatedata',function(){ 
 
      id = $(this).closest('tr').attr('id');// Get the clicked id for deletion 
 
      alert($("#cat").val());// cannot access, undefined 
 
      $.ajax({ 
 
       type:'POST', 
 
       url:'update.php', 
 
       data:{update_id:id, 
 
         cat: $("#cat").val()}, 
 
       success:function(data){ 
 
        alert("updated"); 
 
       } 
 
      })}); 
 
     </script> 
 
    </table> 
 
</html>

回答

1

您正试图通过ID访问输入银价值,这是问题,因为ID不是输入属性提及。

$(document).on('click','.updatedata',function(){ 
      id = $(this).closest('tr').attr('id');// Get the clicked id for deletion 
      cat_val=$(this).closest('tr').find('input[name="cat"]').val(); 
      $.ajax({ 
       type:'POST', 
       url:'update.php', 
       data:{update_id:id, 
         cat: cat_val}, 
       success:function(data){ 
        alert("updated"); 
       } 
      })}); 
0

你的代码是一团糟,你的<head>在哪里?把你的jQuery放在那里。如果您使用的是id,也需要将其他答案添加到输入框id="cat"。从你的代码看起来你正在重复这个输入框,这使得使用id不是你想要做的事情,因为id值只能使用一次。你应该让它成为一个班级,并将class="cat"添加到您的输入框中,并获得值为$('.cat').val();由于您正在使用$(document).on('click','.updatedata',function(){,因此使用此类作为班级更有意义。

+0

根据记录,在大多数情况下,JavaScript是最好位于标签之前,而不是在头部。 – entiendoNull

+0

@Cesar你可以写代码来使用class =“cat”来访问最近的“tr”标签的id –

2

警报($( “#猫”)VAL()); //不能访问,未定义

在上面的第39行,你已经尝试使用 “猫” 的ID,但 '猫' 是提醒值没有定义的 - <input type=text name="cat" value='.$row["category"].'>

解决方案:

变更线 - <input type=text name="cat" value='.$row["category"].'> 用 - <input type=text id="cat" name="cat" value='.$row["category"].'>

注:通过把静态值输入字段,如与尝试 - <input type=text id="cat" name="cat" value='test'>

全部代码解决方案:

<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
 
    <table border="1"> 
 
     <tr> 
 
      <th>categoryId</th> 
 
      <th>category</th> 
 
      <th>Operations</th> 
 
     </tr> 
 
     <?php 
 
       
 
       echo '<tr id="test cat id"><td>Test Cat Id </td><td><input type="text" name="cat" id="cat" value="test"></td><td><button class="updatedata">Update</button></td></tr>'; 
 
     
 
     ?> 
 
     <script> 
 
     $(document).on('click','.deletedata',function(){ 
 
      id = $(this).closest('tr').attr('id'); // Get the clicked id for deletion 
 
      alert(id); 
 
      $.ajax({ 
 
       type:'POST', 
 
       url:'delete.php', 
 
       data:{delete_id:id}, 
 
       success:function(data){ 
 
         window.location.reload(); 
 
       } 
 
      })}); 
 
     $(document).on('click','.updatedata',function(){ 
 
      id = $(this).closest('tr').attr('id');// Get the clicked id for deletion 
 
      alert($("#cat").val());// cannot access, undefined 
 
      $.ajax({ 
 
       type:'POST', 
 
       url:'update.php', 
 
       data:{update_id:id, 
 
         cat: $("#cat").val()}, 
 
       success:function(data){ 
 
        alert("updated"); 
 
       } 
 
      })}); 
 
     </script> 
 
    </table> 
 
</html>