简体   繁体   English

在重定向到单击的链接之前询问用户确认

[英]Asking user confirmation before redirect to a clicked link

I have a long kind wizard form, like a survey in my site. 我有一个很长的向导表格,就像我网站上的一项调查一样。 I want to write a jQuery Function so that when the user click accidentally any link on the page ( except preview and next buttons of the wizard ), it is asked first: are you sure you want to proceed? 我想编写一个jQuery函数,以便当用户无意中单击页面上的任何链接(向导的预览和下一步按钮除外)时,首先会询问您:确定要继续吗? then it is redirected to the link he clicked, if he click cancel, nothing happens.. 然后将其重定向到他单击的链接,如果他单击“取消”,则什么都不会发生。

So far What i have done is to each link of the page except (next & previw) i have added a class link_ridirect so i can grab all the anchor links. 到目前为止,我所做的是对页面的每个链接的了解,除了(下一个和上一个),我添加了一个类link_ridirect这样我可以抓住所有的锚链接。 and stop redirecting. 并停止重定向。

jQuery function is as follow! jQuery函数如下!

<script type="text/javascript">
<!-- HERE IS THE SEARCH FILTER -->
 //<![CDATA[
    var GLOBAL_NAMESPACE = {};
    $(document).ready(function(){
      GLOBAL_NAMESPACE.value_changed = true;
    });

    $(document).ready(function () {
      $('.link_redirect').bind('click',function (e) {
          e.preventDefault();
          if (GLOBAL_NAMESPACE.value_changed){
              var res = confirm('you have unsaved changes. Do you want to continue?');
              if(res){
                  window.location.href = $(this).attr('href');
              }else{
                  console.log('stay on same page...');
              }
          }
      });
    });
//]]>
</script>

So what i want to do is how can i declare a Global variable to keep track of all field state. 因此,我想做的是如何声明全局变量来跟踪所有字段状态。 So if a field changes, to make it true and call the prevent function. 因此,如果某个字段发生更改,则将其设置为true并调用prevent函数。

How about doing this: 如何做到这一点:

 $('a').click(function(){return confirm("are you sure?");});

Place it at the bottom of your html, or in the onload of your page, or in the document ready as you suggested in your OP. 将其放置在html的底部,页面的上载或文档中,如OP中所建议的那样。

edit If you only want to do this if your variable changesDetected is true , then do it like this: 编辑如果您只想在变量changesDetectedtrue执行此操作,请changesDetected方式进行操作:

 $('a').click(function(){return !changesDetected || confirm("are you sure?");});

It looks like you have code to interrupt default A-tag clicks already, so the crux of this is to detect when a field has changed such that you want to ask if they want to save before navigating away ? 看来您已经有代码来中断默认的A标签点击,因此,问题的关键在于检测字段何时更改,以便您在导航之前询问他们是否要保存?

Here's a JSFiddle Detect Field Changes : 这是一个JSFiddle 检测字段更改

It adds an onchange event to all editable fields whcih sets the global stae to true if something changed. 它将全局更改添加到所有可编辑字段的onchange事件,如果某些更改将全局设置设为true。

If the user enters a field then exits without changing, no change is detected. 如果用户输入一个字段,然后不做任何更改就退出,则不会检测到任何更改。

function setup() {
  // bind the change event to all editable fields. Runs on load(or doc ready)
  $("input,select").bind("change",function(e) {
      GLOBAL_NAMESPACE.value_changed = true;
  });  
};

you need to use beforeunload event. 您需要使用beforeunload事件。 This event handled when you go out from page. 当您退出页面时,将处理此事件。

$(this).on("beforeunload", function () {
                return 'are you sure';
            });

if you need, that event called not for preview button and next, you can unbind this event handler. 如果需要,该事件不用于预览按钮,然后单击下一步,可以取消绑定此事件处理程序。

    $('#myPreviewButtonId').click(function()
{
          console.log('preview clicked');
          $(this).unbind("beforeunload");
});

 (function($) { $.fn.checkFileType = function(options) { var defaults = { allowedExtensions: [], success: function() {}, error: function() {} }; options = $.extend(defaults, options); return this.each(function() { $(this).on('change', function() { var value = $(this).val(), file = value.toLowerCase(), extension = file.substring(file.lastIndexOf('.') + 1); if ($.inArray(extension, options.allowedExtensions) == -1) { options.error(); $(this).focus(); } else { options.success(); } }); }); }; })(jQuery); $(function() { $('#image').checkFileType({ allowedExtensions: ['jpg', 'jpeg'], success: function() { alert('Success'); }, error: function() { alert('Error'); } }); }); 
 label { display: block; font-weight: bold; margin-bottom: 0.5em; } 
 <form action="#" method="post" enctype="multipart/form-data"> <div> <label for="image">Upload image (JPEG only)</label> <input type="file" name="image" id="image" /> </div> </form> 

You must prevent the default action on a if result of confirm function is false 如果确认功能的结果为false,则必须防止对默认操作

$(document).ready(function () {
$(".deleteJob").on("click", function (e) {
    if (!confirm("Are you Sure want to delete!")) {
        e.preventDefault();
    }
});

}); });

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

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