简体   繁体   English

javascript重定向确实可以正常工作

[英]javascript redirect does work properly

I have a link which displays a confirm box when user click on it. 我有一个链接,当用户单击它时会显示一个确认框。 If the user click yes then it will lead them to the specified page, otherwise it stays on the same page. 如果用户单击“是”,则会将其引导到指定页面,否则它将停留在同一页面上。 Here is my link and my function code: 这是我的链接和我的功能代码:

<script type='text/javascript'>
   function checkDelete(){
      var i = confirm("Are you sure you want to delete?");
      if(i){
         return true;
      }else{
         return false;
      }
   }
</script>
<a href="operation.php?action=delete&id=<?php echo $id?>" onclick='checkDelete'>Delete</a>

The problem is that it does not stay on the same page even I click "No" or cancel button. 问题是,即使我单击“否”或取消按钮,它也不会停留在同一页面上。 It always redirect to the page operation.php. 它总是重定向到页面operation.php。 But if I write inline javascript code on the onclick even then it works fine. 但是,如果我在onclick上编写内联javascript代码,那么效果很好。

You've got a few problems: 您遇到了一些问题:

The JavaScript code checkDelete simply references the variable checkDelete ; JavaScript代码checkDelete只是引用变量checkDelete ; it does nothing further. 它没有任何进一步的作用。 You probably meant to call it, and further, return the result of calling it: 您可能打算调用它,然后返回调用它的结果:

// in onclick
return checkDelete();

Your use of if with the return statements is redundant. 您将ifreturn语句一起使用是多余的。 Simply return the result of confirm : 只需返回confirm结果:

function checkDelete() {
    return confirm('...');
}

At this point, you could even inline it: 在这一点上,您甚至可以内联它:

// in onclick
return confirm('...');

It is generally frowned upon to have inline JavaScript. 通常没有内置JavaScript。 It is better to bind event handlers from JavaScript. 最好从JavaScript绑定事件处理程序。 For example, if your link had an ID of delete_link , you could use this: 例如,如果您的链接的ID为delete_link ,则可以使用以下代码:

// should only be run when delete_link exists
// (e.g. in a DOM ready handler or a script after delete_link)
// this code does not accommodate for older versions of Internet Explorer
document.getElementById('delete_link').addEventListener('click', function(e) {
    if(!confirm('...')) {
        e.preventDefault();
    }
}, false);

Try onclick="return checkDelete();" 尝试onclick="return checkDelete();" . Using a proper JavaScript expression kind of helps when you try to use JavaScript ;) 当您尝试使用JavaScript时,使用适当的JavaScript表达式会有所帮助;)

At the same time, try changing checkDelete to this: 同时,尝试将checkDelete更改为此:

function checkDelete() {return confirm("Are you sure you want to delete?");}

Try: 尝试:

<a href="operation.php?action=delete&id=<?php echo $id?>" onclick='return checkDelete();'>Delete</a>

<script type='text/javascript'>
   function checkDelete(){
      var i = confirm("Are you sure you want to delete?");
      if(i){
         return true;
      }else{
         return false;
      }
   }
</script>

The <a href=''> tag always redirects to the target. <a href=''>标记始终重定向到目标。 The concept of returning false and cancelling the operation only applies when you're submitting a form. 返回false和取消操作的概念仅在提交表单时适用。

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

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