gpt4 book ai didi

javascript - 如何访问在 php 标签内创建的文本框值以进行 ajax 调用

转载 作者:行者123 更新时间:2023-11-28 18:28:57 28 4
gpt4 key购买 nike

我无法访问我在 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 来提醒值,但“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>

关于javascript - 如何访问在 php 标签内创建的文本框值以进行 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com