简体   繁体   English

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

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

I am playing around with a todo list I learned online. 我在玩我在网上学到的待办事项清单。 Trying to add ajax into it. 尝试将ajax添加到其中。 Works well when I try to add an item. 当我尝试添加项目时效果很好。 When an item is added there's a button says 'Mark as done' so I can click on it and then there will be a 'Delete' button which is used to delete the item from the list and database. 添加项目后,有一个按钮显示“标记为完成”,因此我可以单击它,然后会有一个“删除”按钮,该按钮用于从列表和数据库中删除该项目。

Adding with ajax works fine but after adding, if I click on 'Mark as done' button, the page goes to the url of done-ajax.php?as=done&item=110 where 110 is just the id of the item in database. 使用ajax添加可以正常工作,但是添加后,如果单击“标记为已完成”按钮,则该页面转到done-ajax.php?as=done&item=110的网址,其中110只是数据库中该项的ID。 I have to go back to the index page. 我必须回到索引页面。 And when I go back to index page the item will be marked done already because the 'Mark as done' worked but somehow would go to the url instead of staying in the index page. 当我返回索引页面时,该项目将被标记为已完成,因为“标记为完成”有效,但不知何故,它会转到url,而不是停留在索引页面中。

This is one of the problems found that I have no idea where to look for the result. 这是我不知道在哪里寻找结果的问题之一。

Another problem is if I add the item and refresh the page then clicked 'Mark as done' it wouldn't go to the url instead the ajax works and a 'delete' button would show up but some how the delete button wouldn't work. 另一个问题是,如果我添加项目并刷新页面,然后单击“标记为已完成”,它将不会转到url,而是会显示ajax,并且会显示“删除”按钮,但有些按钮将不起作用。 I have to refresh the page in order for the 'delete' button to work with ajax. 我必须刷新页面才能使“删除”按钮与ajax一起使用。 I checked the attributes and looked around the codes but couldn't seem to find where is causing the problem. 我检查了属性并环顾了代码,但似乎找不到导致问题的原因。

my index codes are as below 我的索引代码如下

<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>

my ajax.js file 我的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;
}
}

(By the way, thanks to few that helped out with the ajax earlier) Thanks a lot to everyone in advance :D (顺便说一句,感谢先前帮助过ajax的人很少)提前非常感谢大家:D

You are binding your event handlers when the page opens / the DOM is ready, on $(document).ready() . 当页面打开/ DOM准备就绪时,您正在$(document).ready()上绑定事件处理程序。 Then you add new items using ajax but the links in these items will not have your event handlers bound to them. 然后,您可以使用ajax添加新项目,但这些项目中的链接将不会绑定事件处理程序。

You can use event delegation to make sure that the events are also automatically bound to newly added items. 您可以使用事件委托来确保事件也自动绑定到新添加的项目。

You can do that changing: 您可以进行以下更改:

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

to something like: 像这样:

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

where the $(".list") can be any element that contains the newly added elements and that is on the page when this code executes. $(".list")可以是包含新添加的元素的任何元素,并且在执行此代码时位于页面上。

This applies to all event handlers that you want to bind to elements that are not yet on the page when the DOM is ready. 这适用于在DOM准备好后要绑定到页面上尚未存在的元素的所有事件处理程序。

暂无
暂无

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

相关问题 在刷新之前,我的AJAX呼叫无法在下一页上使用 - My AJAX call won't work on next page untill I hit refresh 除非我刷新页面,否则集成在选项卡中的手风琴不起作用 - Accordion integrated in tab don't work unless i refresh the page 我正在使用laravel 5.5 Ajax执行更新和删除页面不会自动刷新 - Ajax perform update and delete page won't auto refresh i'm using laravel 5.5 当我刷新页面时,Jquery 代码无法完成 - When I refresh the page the Jquery code doesn't work done 为什么我的删除按钮不起作用,除非我在不同的 div 中使用不同的删除按钮? - Why does my delete button not work unless I use a different delete button in a different div? 除非刷新页面,否则“重置”按钮将不起作用,否则会给我一个错误(React.JS) - “Reset” button won't work unless it refreshes page, and gives me an error otherwise (React.JS) 为什么除非刷新或使用location.href,否则ajax无法正常工作? - Why ajax doesn't work unless I refresh or use location.href? Vue data() object 输入 v-model 不起作用,除非我刷新页面 - Vue data() object Input v-model doesn't work unless I refresh the page Selenium 将不起作用,除非我实际查看 Web 页面(也许是 JavaScript 的反爬虫机制?) - Selenium won't work unless I actually look at the Web page (perhaps anti-crawler mechanism by JavaScript?) 除非刷新页面,否则Rails ajax的第一个请求不会显示 - Rails ajax first request not showing up unless I refresh the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM