繁体   English   中英

我的完成按钮以某种方式转到空白页,除非刷新-ajax,否则删除将不起作用

[英]my done button somehow went to a blank page and delete won't work unless I refresh -ajax

我在玩我在网上学到的待办事项清单。 尝试将ajax添加到其中。 当我尝试添加项目时效果很好。 添加项目后,有一个按钮显示“标记为完成”,因此我可以单击它,然后会有一个“删除”按钮,该按钮用于从列表和数据库中删除该项目。

使用ajax添加可以正常工作,但是添加后,如果单击“标记为已完成”按钮,则该页面转到done-ajax.php?as=done&item=110的网址,其中110只是数据库中该项的ID。 我必须回到索引页面。 当我返回索引页面时,该项目将被标记为已完成,因为“标记为完成”有效,但不知何故,它会转到url,而不是停留在索引页面中。

这是我不知道在哪里寻找结果的问题之一。

另一个问题是,如果我添加项目并刷新页面,然后单击“标记为已完成”,它将不会转到url,而是会显示ajax,并且会显示“删除”按钮,但有些按钮将不起作用。 我必须刷新页面才能使“删除”按钮与ajax一起使用。 我检查了属性并环顾了代码,但似乎找不到导致问题的原因。

我的索引代码如下

<div class="list">
<h1 class="header">ET's To do lists.</h1>

<?php if(!empty($items)): ?>
<ul class="items">
    <?php foreach($items as $item): ?>
    <li>
                    <span class="item<?php echo $item['done'] ? ' done' : '' ?>">
                        <?php echo $item['todoText']; ?>
                    </span>
        <?php if($item['done']): ?>
        <a href="delete-ajax.php?as=delete&item=<?php echo $item['id']; ?>" class="delete-button">Delete Task</a>
        <?php endif; ?>
        <?php if(!$item['done']): ?>
        <a href="done-ajax.php?as=done&item=<?php echo $item['id']; ?>" class="done-button">Mark as done</a>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php else: ?>
<p class="empty">You haven't added any items yet.</p>
<ul class="items"></ul>
<?php endif ?>

<form class="item-add" action="add.php" method="post">
    <input type="text" name="todoText" placeholder="Type a new item here." class="input" autocomplete="off" required>
    <input type="submit" value="Add" class="submit">
</form>
</div>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/ajax.js"></script>

我的ajax.js文件

$(document).ready(function() {

//Action when submit is clicked
$(".submit").click(function(e){
var todoText = $("input[name='todoText']").val();
e.preventDefault();

//Ajax for adding todoText
$.ajax({
method: "POST",
url: "add-ajax.php",
data: {todoText: todoText},
dataType: "json"
})
.done(function(data){
$('p.empty').empty();
$('input.input').val('');
$('ul.items').append('<li>'+todoText+' '+
    '<a href="done-ajax.php?as=done&item=' + data.id +
                '" class="done-button">Mark as Done</a></li>');
})
});

//Action when done button is clicked
$(".done-button").click(function(e){
e.preventDefault();

//making sure only work with the current element
var $clicked = $(this);

//get the href attribute value and parse it to get the item # which is the item's id
var attrValue = $clicked.attr('href');
var parseAttrValue = attrValue.split('&');
var parseItem = parseAttrValue[1].split('=');
var item = parseItem[1];

//Ajax for Mark as Done Button
$.ajax({
method: "GET",
data:{as: 'done', item: item},
url: "done-ajax.php"
})
.done(function(){
$clicked.prev().addClass('done');
$clicked.removeClass('done-button').empty();
$clicked.addClass('delete-button').text('Delete Task');
$clicked.removeAttr('href');
$clicked.attr('href','delete-ajax.php?as=delete&item='+item);
});
});

//Action when delete button is clicked
$(".delete-button").click(function(e){
e.preventDefault();

//making sure only work with the current element
var $clicked = $(this);

//get the href attribute value and parse it to get the item # which is the item's id
var attrValue = $clicked.attr('href');
var parseAttrValue = attrValue.split('&');
var parseItem = parseAttrValue[1].split('=');
var item = parseItem[1];

//Ajax for Mark as Done Button
$.ajax({
method: "GET",
data:{as: 'delete', item: item},
url: "delete-ajax.php"
})
.done(function(){
$clicked.closest('li').remove();
$clicked.remove();
});
});
});

my done-ajax.php file


<?php

require_once 'app/init.php';

if (isset($_GET['as'], $_GET['item']))
{
    $as = $_GET['as'];
    $item = $_GET['item'];

    switch ($as) {
        case 'done':
            $doneQuery = $db->prepare("
UPDATE phptodolist_items
SET done = 1
WHERE id = :item
AND user = :user
");
break;
}
}


my delete.php file

<?php

require_once 'app/init.php';

if (isset($_GET['as'], $_GET['item']))
{
    $as = $_GET['as'];
    $item = $_GET['item'];

    switch ($as) {
        case 'delete':

            $doneQuery = $db->prepare("
DELETE FROM phptodolist_items
WHERE id = :item
AND user = :user
");
break;
}
}

(顺便说一句,感谢先前帮助过ajax的人很少)提前非常感谢大家:D

当页面打开/ DOM准备就绪时,您正在$(document).ready()上绑定事件处理程序。 然后,您可以使用ajax添加新项目,但这些项目中的链接将不会绑定事件处理程序。

您可以使用事件委托来确保事件也自动绑定到新添加的项目。

您可以进行以下更改:

$(".delete-button").click(function(e){
e.preventDefault();
...

像这样:

$(".list").on('click', '.delete-button', function(e){
  e.preventDefault();
  ...

$(".list")可以是包含新添加的元素的任何元素,并且在执行此代码时位于页面上。

这适用于在DOM准备好后要绑定到页面上尚未存在的元素的所有事件处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM