取消确认按钮问题

时间:2017-09-20 20:57:39

标签: javascript jquery

当用户点击下面的链接时,他们会收到确认提示,但取消按钮仍然会将其发送到URL。我正在检查他们是否填写了任何文本字段,以警告他们数据丢失。

编辑:我必须使用onclick

<a class="owl-prev button disabled" href="blabla.com" onclick="checking()"> back</a>


jQuery(window).load(function(){
checking = function() {

    var empty = 0;
   jQuery('.tmcp-textfield').each(function(){
       if (this.value != "") {
           empty++;
       } 
    })
   if(empty > 1)
   {
    return confirm('Are you sure?');
   }

}});

3 个答案:

答案 0 :(得分:0)

如果验证设置为false,则更改onclick函数的返回值以返回false abd将检查更改为<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div> <div class="collapse navbar-collapse" > <ul id="my-navbar-ul" class="nav navbar-nav"></ul> </div> </div> <script id="list" type="text/template"> <li class="dropdown"> <a rel="nofollow" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" id="namelist"> Select a Name<span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a rel="nofollow" href="#" id="mark">Mark</a></li> <li><a rel="nofollow" href="#" id="allan">Allan</a></li> <li><a rel="nofollow" href="#" id="derek">Derek</a></li> <li><a rel="nofollow" href="#" id="brian">BRIAN</a></li> </ul> </li> </script>

<强>演示:

&#13;
&#13;
empty > 0
&#13;
checking = function() {
  var empty = 0;
  jQuery('.tmcp-textfield').each(function() {
    if (this.value != "") {
      empty++;
    }
  })
  if (empty > 0) {
    return confirm('If you go back, all typed data will be lost. Do you still want to go back?');
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

您需要阻止默认事件,然后手动重定向(empty > 1)

尝试event.preventDefault()

有关详细说明,请参阅https://stackoverflow.com/a/9945582/5775828

答案 2 :(得分:-2)

我会尝试删除锚标记并使用其他内容来阻止默认行为。试试按钮标签?