Javascript 如何使用jquery change()更新行状态并显示在相应的表中

Javascript 如何使用jquery change()更新行状态并显示在相应的表中,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,在使用标记更改表中特定行的状态时,我面临一个主要问题 我使用的是jquery Ajax,因此,当单击时,问题就出在这一行: $(“#显示”).html(结果) 这是怎么回事 您正在为每一行创建displayid,这不是很好的做法,特别是在您的特定应用程序中 这个问题有多种解决方案 1) (“#显示“,$(this).parent().parent()).html(结果) 在这里,您将把操作应用于特定的ID,该ID属于接收到更改操作的特定类的父类的父类 2) 为每一行指定显示行的唯一id 例如:-

在使用
标记更改表中特定行的状态时,我面临一个主要问题


我使用的是jquery Ajax,因此,当单击
时,问题就出在这一行:

$(“#显示”).html(结果)

这是怎么回事

您正在为每一行创建
display
id,这不是很好的做法,特别是在您的特定应用程序中

这个问题有多种解决方案

1)

(“#显示“,$(this).parent().parent()).html(结果)

在这里,您将把操作应用于特定的
ID
,该ID属于接收到更改操作的特定类的父类的父类

2)

为每一行指定显示行的唯一id

例如:-


是,
$(“#display”).html(结果)中存在问题行,因为id(
$(“#display”)
总是选择在DOM中找到的第一个元素,您可以通过以下代码修复它-

<script>
  $(document).ready(function(){
            $(".selectstatus").change(function(){
                // make jquery object that make reference to select in which click event is clicked
                $this = $(this);
                var statusname = $(this).val();
                var getid = $(this).attr("status-id");
                //alert(displid);

                $.ajax({
                    type:'POST',
                    url:'ajaxreceiver.php',
                    data:{statusname:statusname,getid:getid},
                    success:function(result){
                        // this refer to the ajax callback function so we have to use $this which is initialized before ajax call
                        $($this).parents('tr').find("#display").html(result);
                    }
                });
            });
        });
    </script>

$(文档).ready(函数(){
$(“.selectstatus”).change(函数(){
//生成jquery对象,该对象引用选择在其中单击click事件
$this=$(this);
var statusname=$(this.val();
var getid=$(this.attr(“状态id”);
//警报(displaid);
$.ajax({
类型:'POST',
url:'ajaxreceiver.php',
数据:{statusname:statusname,getid:getid},
成功:功能(结果){
//这是指ajax回调函数,因此我们必须使用$this,它在ajax调用之前初始化
$($this).parents('tr').find(#display”).html(结果);
}
});
});
});

您能否在phpy中显示更新查询代码第二个示例
@PavanBaddi祝您好运,但请注意,我的代码中有一个类型已修复,
$(“#display”+getid).html(result)必须是
$(“#display"+getid).html(结果)
    <?php
    include('processing.php');
    $newobj = new processing();

    if(isset($_POST['statusname'],$_POST['getid'])){
        $statusid = $_POST['statusname'];
        $id = $_POST['getid'];

        $newobj->getdata($statusid,$id);
    }
?>
  <?php
    class processing{
        private $link;

        function __construct(){
            $this->link= new mysqli('localhost','root','','example');
            if(mysqli_connect_errno()){
                die ("connection failed".mysqli_connect_errno());
            }
        }

        function display(){
            $sql = $this->link->stmt_init();
            $id=1;
            if($sql->prepare("SELECT id,productname,status FROM ajaxselect")){
                $sql->bind_result($id,$productname,$status);
                if($sql->execute()){
                    while($sql->fetch()){   
            ?>
                        <tr>
                            <td><?php echo $id;?></td>
                            <td><?php echo $productname;?></td>
                            <td><p id="display"><?php echo $status;?></p></td>
                            <td>
                                <select status-id=<?php echo $id;?> id="selectstatus" class="selectstatus">
                                    <option>Pending</option>
                                    <option>Delivered</option>
                                    <option>Cancelled</option>
                                    <option>Amount Paid</option>    
                                </select>
                            </td>
                        </tr>
            <?php   
                    }
                }
            }
        }

        function getdata($statusid,$id){
            $sql = $this->link->stmt_init();
            if($sql->prepare("UPDATE ajaxselect SET status=? WHERE id=?")){
                $sql->bind_param('si',$statusid,$id);
                if($sql->execute()){
                    echo $statusid;
                }
                else
                {
                    echo "Update Failed";
                }
            }
        }
    }
?>
<script>
  $(document).ready(function(){
            $(".selectstatus").change(function(){
                // make jquery object that make reference to select in which click event is clicked
                $this = $(this);
                var statusname = $(this).val();
                var getid = $(this).attr("status-id");
                //alert(displid);

                $.ajax({
                    type:'POST',
                    url:'ajaxreceiver.php',
                    data:{statusname:statusname,getid:getid},
                    success:function(result){
                        // this refer to the ajax callback function so we have to use $this which is initialized before ajax call
                        $($this).parents('tr').find("#display").html(result);
                    }
                });
            });
        });
    </script>