1

我正在创建一个待办事项列表,它可以排序、添加要做的事情和删除要做的事情。我正在使用 Jquery。我是 javascript 和 php 的新手。

(1) 点击删除图标。它应该在后端与 PHP mysql 连接以将其删除。我尝试了很多方法来链接它们以删除它们的整行,但所有尝试均不成功。

(2) 单击复选框后,除非取消选中,否则应始终保持选中状态。我也不知道该怎么做。我尝试了所有 youtube 和 stack 方法。

我的数据库是清单表是清单我有 checklist_id、checkbox、thingtodo、sortable_order

我的代码:

$("#projects").on("click", "input[type=checkbox]", function(){
        $(this).closest("li").animate(function(){
            $(this).checked()
        });
    });

    $("#projects").on("click", ".ui-icon-trash", function(){
        $(this).closest("li").slideUp(function(){
            $(this).remove();

        });
    });
4

1 回答 1

0

HTML:每个待办事项列表行都应具有与您的数据库行相对应的唯一标识符(checklist_id)

JS:

$("#projects").on("click", ".ui-icon-trash", function () {
$(this).closest("li").slideUp(function () {
    // get the associated id for that row
    var checklistID = $(this).data('id'); // change this to whichever tag you are using in your html

    // this is the part that connects to your php code, your php code will be the one responsible to connect to mysql
    $.ajax({
    url: 'http://urltophp.com/deleteChecklist', // url to your php function
    data: {id : checklistID },
    type: 'post',
    dataType: "json",
    success: function (response) {
        if (response) {
        $(this).remove();
        } else {
        // show an error if you like
        }
    }
    });
});
});

PHP:

function deleteRow($params) {
$query = "DELETE FROM checklist WHERE checklist_id = " . $params['id'];

// call database to exceute this query

return true;  // or false depending if your query was successful or not
}
于 2017-09-11T13:09:31.527 回答