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

标签 javascript php jquery css ajax

我正在尝试在单击特定单元格后对其进行编辑。当我当前单击一个单元格时,它会将所有 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/

相关文章:

javascript - 如何保存在线 html 中的更改

php - 如何从 codeIgniter 3.x.x 中的两个不同表中获取值

javascript - 使用复选框和多个选择器更改功能

javascript - 替代 CSS onclick 函数

javascript - 使用 Javascript 根据星期几和一天中的时间显示 HTML 元素

Angular 中的 JavaScript |事件绑定(bind)

php - 如何替换已解码的 Non-breakable space (nbsp)

php - 为什么将posted变量打印到html代码中会导致XSS?

javascript - ASP.NET MVC JsonResult 返回 500

javascript - 无法使用 CSS-Approach 或 JQuery-Apprach 将必需的验证添加到文本区域