drxp993551 2014-07-24 08:22
浏览 20

PHP和jQuery删除动态列表项

I hope someone will be able to shed some light on why I am having this issue. I have created a basic list that is generated from a MySQL database using a PHP class. I am then trying to use some basic jQuery to remove a list item. I could not get it working for a long time eventually finding that if I moved the jQuery include to the end of the HTML file it works but not with it included at the top of the script as I would normally aim to do. I had thought that the jQuery document ready function should have resolved this but it is not for some reason.

Here is the jQuery (very basic as said):

$(document).ready(function(){
    $(".completeButton").click(function(){
        $(this).closest('li').remove();
    });
});

Here is the HTML page body (notice I have the jQuery include near the end rather than in the header where it does not work and remove the item, it works fine with it here though?):

<?php
session_start();
if(isset($_SESSION['user'])) {
    include "header.php";
?>
<h2>Task List</h2>
<?php
    include_once "DatabaseConn.php";
    include_once "taskList.class.php";

    $sql = "SELECT `GV_Employee`.`requests`.`req_ID` , `GV_Employee`.`equipment`.`description` , `GV_Employee`.`employees`.`first_name`, `GV_Employee`.`employees`.`last_name` 
        FROM requests
        RIGHT JOIN equipment
        ON requests.eq_ID=equipment.eq_ID
        RIGHT JOIN employees
        ON requests.emp_ID=employees.emp_ID
        ORDER BY requests.req_ID;";

    $stmt = $db->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchALL();

    foreach ($results as $row){
        $task[] = new tasklist($row);
    }
    foreach($task as $item){
        echo $item;
    }
?>
<script src="tasklist.js"></script>
<?php
}else {
    session_destroy();
    header("Location: http://dunc2/dylanr/Starters&Leavers/index.php");
    //header("Location: http://192.168.0.4/Starters&Leavers/index.php");
    exit;
}
?>

Here is the class for the dynamic list:

<?php 

class taskList {
    private $taskData;

    public function __construct($task){
        if(is_array($task))
            $this->taskData = $task;
    }

    public function __toString(){
        return '<li id="task-'.$this->taskData['req_ID'].'" class="taskList">
                <div class="text">'.$this->taskData['first_name'].' '.$this->taskData['last_name'].
                    ' requires: '.$this->taskData['description'].'</div>
                    <div id="button">
                        <a href="#" class="completeButton" onclick="completeButton();">Complete</a>
                        <a href="#" class="taskButton">Priority</a>
                    </div>
            </li>';
    }
}

?>

I hope this question makes sense, I tried searching the forum but could not find a topic which covered this exact situation apologies if there is one.

Many thanks James

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)