簡體   English   中英

如何訪問在php標記內創建的textbox值以進行ajax調用

[英]How to access the textbox value created inside php tag to the ajax call

我無法訪問在ajax調用的php標記內創建的文本框值。 當我在ajax中打印文本框值時,將其稱為“未定義”,因此有任何方法可以訪問文本框值。 請參閱第39行的代碼中的注釋。

 <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> 

alert($(“#cat”)。val()); //無法訪問,未定義

在第39行的上方,您嘗試使用“ cat” id來警告值,但在- <input type=text name="cat" value='.$row["category"].'>未定義'cat' <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> 

您正在嘗試通過ID訪問輸入BOC值,這是有問題的,因為在輸入屬性中未提及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");
                }
            })});

您的代碼一團糟,您的<head>哪里? 把你的jQuery放在那里。 與其他答案一樣,如果使用id則需要將其添加到輸入框id="cat" 從您的代碼看來,您正在重復此輸入框,這使得使用id並不是您想要執行的操作,因為id值只能使用一次。 您應該改為使其成為一個類,並在輸入框中添加class="cat"並將其值獲取為$('.cat').val(); 由於您使用的是$(document).on('click','.updatedata',function(){將其用作類更有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM