简体   繁体   English

if / else语句没有调用函数?

[英]if/else statement not calling a function?

I am going insane here. 我在这里疯了。 I have searched and searched and tried a number of different ways, i'm not getting any errors, but I cant get the if/else statement to call the function it is supposed to be calling. 我搜索并搜索并尝试了许多不同的方法,我没有得到任何错误,但我不能得到if / else语句来调用它应该调用的函数。 the function itself works fine if called otherwise, just not inside the if statement, what is going on?? 如果另外调用函数本身工作正常,只是不在if语句中,发生了什么?

oops, ok I deleted the "var" in the second part, but its still behaving a bit weird -- what I want it to do, is when the confirm delete button is pushed, to delete the item and close the modal. 哎呀,好吧我删除了第二部分中的“var”,但它仍然表现得有些奇怪 - 我想要它做的是,当按下确认删除按钮时,删除项目并关闭模态。 what its doing instead, is closing the modal, i have to click delete a second time, and then it deletes the item, and the modal quickly pops up the closes itself. 相反,它正在关闭模态,我必须再次单击删除,然后它删除项目,模式快速弹出关闭本身。

edit: I changed my code to remove event listeners and use onclick in html instead to see if that was the issue, but its not. 编辑:我更改了我的代码以删除事件侦听器并在html中使用onclick来查看是​​否是问题,但事实并非如此。 im still having the same problem. 我仍然有同样的问题。

var clickedCancel = false;
var clickedDelete = false;
//buttons false by defaul
//if cancel button is clicked, set clickedcancel to true.

//if confirm delete button is clicked, set clicked delete to true
function cancelDeletion(){
  clickedCancel = true;
}

function confirmDeletion(){
  clickedDelete = true;
}

function deleteMovie(x){
  //open modal
$('#modalContainerDelete').modal('toggle');


  if (clickedDelete == true){
    movies.splice(x,1);
  addMovieToTable();

  $('#modalContainerDelete').modal('hide');
} else if (clickedCancel == true) {

  $('#modalContainerDelete').modal('hide');
}

};

The biggest issue is, my assignment is I need a delete button that removes a specific item from an array, but I have to code the delete button into a row, and delete the same row the delete button is on, and that part is coded this way: 最大的问题是,我的任务是我需要一个从数组中删除特定项的删除按钮,但我必须将删除按钮编码成一行,并删除删除按钮所在的同一行,并且该部分已编码这条路:

function addMovieToTable(){
table.innerHTML = "";
for (i=0;i<movies.length;i++){

var row = table.insertRow(0);
var cellTitle = row.insertCell(0);
var cellDirector = row.insertCell(1);
var cellDelete = row.insertCell(2);
cellTitle.innerHTML= movies[i].title;
cellDirector.innerHTML= movies[i].director;
var deleteStuff = "<button onclick='deleteMovie(" + i + ")'>Delete</button>";
cellDelete.innerHTML = deleteStuff;
}

}

the part with the inner html, and the i variable, is what I am trying to code around, I am supposed to make it so before deleting anything, a modal pops up, but this is the main reason im having issues is im trying to code around that without ruining the set up. 内部html和i变量的部分是我想要编写的代码,我应该在删除任何东西之前做到这一点,弹出一个模态,但这是我遇到问题的主要原因是我试图在不破坏设置的情况下编写代码。

Use the following code 使用以下代码

var clickedCancel = false;
var clickedDelete = false;
//buttons false by defaul
//if cancel button is clicked, set clickedcancel to true.
var cancelDeletion = document.getElementById("cancelDeletion");
cancelDeletion.addEventListener("click", function () {
    clickedCancel = true;
});
//if confirm delete button is clicked, set clicked delete to true
var confirmDeletion = document.getElementById("deleteConfirm");
confirmDeletion.addEventListener("click", function () {
    clickedDelete = true;
});

function deleteMovie(x) {
    //open modal
    $('#modalContainerDelete').modal('toggle');
    if (clickedDelete == true) {
        movies.splice(x, 1);
        addMovieToTable();
        $('#modalContainerDelete').modal('hide');
    } else if (clickedCancel == true) {
        $('#modalContainerDelete').modal('hide');
    }
};

...... ......

you declare clickedCancel clickedDelete twice remove the second var and it will work fine. 你声明clickedCancel clickedDelete两次删除第二个var,它将正常工作。 Also : its better if you stop the event listener when you done with it and fire it just when you need it to avoid multiple calls 另外:如果你在完成它时停止事件监听器并且只在你需要时触发它以避免多次调用它会更好

Edit : 编辑:

Remember if user click cancel button and then try to click delete button it will do nothing because eventlistener still remember the old option ( which is cancel ) thats why you have to find a way to turn that listener off and on to avoid multiple calls. 请记住,如果用户单击取消按钮,然后尝试单击删除按钮,它将不执行任何操作,因为eventlistener仍然记住旧选项(取消),这就是为什么你必须找到一种方法来关闭和打开该监听器以避免多次调用。 thats why when you try to delete after pressing cancel it didn't work for you ... 这就是为什么当你按下取消后尝试删除它为你不起作用...

this is an example : 这是一个例子:

    function delete_comment(element, event){
        event.preventDefault();
        // get the post id
        var post_id    = $(element).parents("div.IdHolderDiv").attr("id");
        // get comment id
        var comment_id = $(element).parents('div.singleCommentBox').find('div.CommentdialogboxUnderPOST').attr('id');
    // here is the confirmation button (where user press cancel or delete)
        var confirmBox  = "<div id='confirm_delete_comment' class='w3-modal'>";
            confirmBox += "<div class='alert warning' >";
            confirmBox += "<center><br><br>";
            confirmBox += "<p>Are you sure you want to delete this Comment!</p>";
            confirmBox += "<button class='w3-btn w3-ripple w3-red' > Delete</button>";
            confirmBox += "<button class='w3-btn w3-ripple w3-yellow' > Cancel</button>";
            confirmBox += "<br><br></center></div></div>";
// append the confirmation box to your Dom in an empty div
        $('#confirm_delete').append(confirmBox);
        // make that div visable
        // confirmation message.
        $('#confirm_delete_comment').css('display', 'block');

        // wait user confirmation.
// and here you fire the event listener
        $("#confirm_delete_comment").on("click", "button", function(event){
            event.preventDefault(); 
            var confirm = $(this).text().trim();
// check if user says cancel or delete and then you can turn the listener off
            if( confirm == "Delete" ){
                $.post('public/ajax/delete_comment.php', {post_id:post_id, comment_id:comment_id}, function(data){
                    $.post('public/ajax/comment_get.php', {post_id:post_id}, function(data){
                        // change the counter (comment number at the id of that specific post for full modle and index page)
                        $('#post_'+post_id+'_comments').text(data); 
                        if(indexPage.length){
                            $('#showPostFullScreen #post_'+post_id+'_comments').text(data); 
                        }else if(profilePage.length){
                            $('#showPostFullScreenProfile #post_'+post_id+'_comments').text(data); 
                        }
                        // reload comments.
                        if( $('#modal_post').css('display') == 'none' ){
                            load_comments(post_id); 
                        }else if( $('#modal_post').css('display') == 'block' ){
                            load_commentsFull(post_id);

                        }

                    });
                    // get the notifications
                    load_notifications();
                });
                // remove listner. for older than 1.7 $('#infoarea_display_editon button').unbind('click');
                $('#confirm_delete_comment').off('click button');
                $('#confirm_delete').empty();
            }else if( confirm == "Cancel" ){
                $('#confirm_delete_comment').off('click button');
                $('#confirm_delete').empty();
            }
        });
    }

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

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