我的完成按钮以某种方式进入了一个空白页并删除了不会工作,除非我刷新-ajax

时间:2015-06-15 07:42:26

标签: javascript php jquery ajax button

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

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

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

另一个问题是,如果我添加项目并刷新页面,然后点击“标记为已完成”#39;它不会转到网址,而是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

1 个答案:

答案 0 :(得分:1)

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

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

你可以改变:

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

类似于:

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

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

这适用于您希望在DOM准备就绪时绑定到页面上尚未存在的元素的所有事件处理程序。