gpt4 book ai didi

javascript - 使用 JQuery 和 Ajax 访问单击的单元格而不是所有单元格

转载 作者:行者123 更新时间:2023-11-28 12:22:07 25 4
gpt4 key购买 nike

我正在尝试在单击特定单元格后对其进行编辑。当我当前单击一个单元格时,它会将所有 td 变成文本框。我只希望点击的单元格执行此操作。我怎样才能只访问点击的单元格?这是我当前的代码:(暂时忽略 .change 函数;我还没有修复它。

 $(document).ready(function()
{
$(".edit_td").click(function()
{
$(this).children(".text").hide();
$(this).children(".editbox").show();
}).change(function()
{
var id=$(this).parent();
var field=$("#input_"+ id).val();
var text=$("#span_" + id).val();
var dataString = 'id='+ id +'&field='+ field +'&text='+ text;
//$("#first_"+ID).html('<img src="load.gif" />'); // Loading image

if(input != text)
{
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#first_"+ID).html(first);
$("#last_"+ID).html(last);
}
});
}
else
{
alert('Enter something.');
}
});

// Edit input box click action
$(".editbox").mouseup(function()
{
return false
});

// Outside click action
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});

});

这是我的 PHP 代码:

   public function displayTable($table)
{
//connect to DB
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

echo "<table id='table' border='1'>"; //start an HTML table

$dbtable = $table;
$fields =array();
$result = mysqli_query($con, "SHOW COLUMNS FROM ".$dbtable);

//fill fields array with fields from table in database
while ($x = mysqli_fetch_assoc($result))
{
$fields[] = $x['Field'];
}

$fieldsnum = count($fields); //number of fields in array

//create table header from dbtable fields
foreach ($fields as $f)
{
echo "<th>".$f."</th>";
}

//create table rows from dbtable rows
$result = mysqli_query($con, "SELECT * FROM ".$dbtable);

while ($row = mysqli_fetch_array($result))
{
$rowid = $row[$fields[0]];
echo "<tr class='edit_tr' id='".$rowid."'>";
foreach ($fields as $f)
{
echo "<td class='edit_td'><span id='span_".$rowid."' class='text'>".$row[$f]."</span>
<input type='text' value='".$row[$f]."' class='editbox' id='input_".$rowid."'/> </td>";
}
$rowid++;
echo "</tr>";
}

echo "</table>"; //close the HTML table

//close connection
mysqli_close($con);
}

最佳答案

您当前告诉所有 texteditbox 在您单击某些内容时分别隐藏和显示。您要做的只是使与您单击的元素相关的隐藏和显示。

为此,您需要按照以下方式做一些事情

$(".edit_tr").click(function() {
$(this).children(".text").hide();
$(this).children(".editbox").show();
}) // your normal stuff can go here.

这将仅抓取和修改类 edit_tr 的子级并显示/隐藏您想要的元素。

关于javascript - 使用 JQuery 和 Ajax 访问单击的单元格而不是所有单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18805918/

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