gpt4 book ai didi

javascript - jQuery.AreYouSure 插件不工作

转载 作者:行者123 更新时间:2023-11-30 00:16:42 26 4
gpt4 key购买 nike

过去几天我一直在尝试正确安装 jQuery.AreYouSure?结果不一致的插件。我决定根据这篇文章中的信息使用这个插件:Warn user before leaving web page with unsaved changes .我还阅读了 GitHub 上的插件文档:jQueryAreYouSure

这是我所做的:

  1. 我刷新页面。
  2. 我一直在将代码从 custom.js 文件复制到 chrome 的控制台中。
  3. 我对表格进行更改
  4. 我点击刷新按钮或其他网站链接不保存就离开。

我的结果:有时我会收到警报消息,但更多时候我没有收到。当我收到警报时,我也不能不复制或理解。有时它可能会在我第一次点击离开页面时出现,而有时它可能会在我点击几次后出现。

javascripts/application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require areyousure/areyousure
//= require_tree .

javascripts/areyousure/areyousure.js:

/*!
* jQuery Plugin: Are-You-Sure (Dirty Form Detection)
* https://github.com/codedance/jquery.AreYouSure/
*
* Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Author: chris.dance@papercut.com
* Version: 1.9.0
* Date: 13th August 2014
*/
(function($) {

$.fn.areYouSure = function(options) {

var settings = $.extend(
{
'message' : 'You have unsaved changes!',
'dirtyClass' : 'dirty',
'change' : null,
'silent' : false,
'addRemoveFieldsMarksDirty' : false,
'fieldEvents' : 'change keyup propertychange input',
'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
}, options);

var getValue = function($field) {
if ($field.hasClass('ays-ignore')
|| $field.hasClass('aysIgnore')
|| $field.attr('data-ays-ignore')
|| $field.attr('name') === undefined) {
return null;
}

if ($field.is(':disabled')) {
return 'ays-disabled';
}

var val;
var type = $field.attr('type');
if ($field.is('select')) {
type = 'select';
}

switch (type) {
case 'checkbox':
case 'radio':
val = $field.is(':checked');
break;
case 'select':
val = '';
$field.find('option').each(function(o) {
var $option = $(this);
if ($option.is(':selected')) {
val += $option.val();
}
});
break;
default:
val = $field.val();
}

return val;
};

var storeOrigValue = function($field) {
$field.data('ays-orig', getValue($field));
};

var checkForm = function(evt) {

var isFieldDirty = function($field) {
var origValue = $field.data('ays-orig');
if (undefined === origValue) {
return false;
}
return (getValue($field) != origValue);
};

var $form = ($(this).is('form'))
? $(this)
: $(this).parents('form');

// Test on the target first as it's the most likely to be dirty
if (isFieldDirty($(evt.target))) {
setDirtyStatus($form, true);
return;
}

$fields = $form.find(settings.fieldSelector);

if (settings.addRemoveFieldsMarksDirty) {
// Check if field count has changed
var origCount = $form.data("ays-orig-field-count");
if (origCount != $fields.length) {
setDirtyStatus($form, true);
return;
}
}

// Brute force - check each field
var isDirty = false;
$fields.each(function() {
$field = $(this);
if (isFieldDirty($field)) {
isDirty = true;
return false; // break
}
});

setDirtyStatus($form, isDirty);
};

var initForm = function($form) {
var fields = $form.find(settings.fieldSelector);
$(fields).each(function() { storeOrigValue($(this)); });
$(fields).unbind(settings.fieldEvents, checkForm);
$(fields).bind(settings.fieldEvents, checkForm);
$form.data("ays-orig-field-count", $(fields).length);
setDirtyStatus($form, false);
};

var setDirtyStatus = function($form, isDirty) {
var changed = isDirty != $form.hasClass(settings.dirtyClass);
$form.toggleClass(settings.dirtyClass, isDirty);

// Fire change event if required
if (changed) {
if (settings.change) settings.change.call($form, $form);

if (isDirty) $form.trigger('dirty.areYouSure', [$form]);
if (!isDirty) $form.trigger('clean.areYouSure', [$form]);
$form.trigger('change.areYouSure', [$form]);
}
};

var rescan = function() {
var $form = $(this);
var fields = $form.find(settings.fieldSelector);
$(fields).each(function() {
var $field = $(this);
if (!$field.data('ays-orig')) {
storeOrigValue($field);
$field.bind(settings.fieldEvents, checkForm);
}
});
// Check for changes while we're here
$form.trigger('checkform.areYouSure');
};

var reinitialize = function() {
initForm($(this));
}

if (!settings.silent && !window.aysUnloadSet) {
window.aysUnloadSet = true;
$(window).bind('beforeunload', function() {
$dirtyForms = $("form").filter('.' + settings.dirtyClass);
if ($dirtyForms.length == 0) {
return;
}
// Prevent multiple prompts - seen on Chrome and IE
if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
if (window.aysHasPrompted) {
return;
}
window.aysHasPrompted = true;
window.setTimeout(function() {window.aysHasPrompted = false;}, 900);
}
return settings.message;
});
}

return this.each(function(elem) {
if (!$(this).is('form')) {
return;
}
var $form = $(this);

$form.submit(function() {
$form.removeClass(settings.dirtyClass);
});
$form.bind('reset', function() { setDirtyStatus($form, false); });
// Add a custom events
$form.bind('rescan.areYouSure', rescan);
$form.bind('reinitialize.areYouSure', reinitialize);
$form.bind('checkform.areYouSure', checkForm);
initForm($form);
});
};
})(jQuery);

我创建了 javascripts/custom.js:

$('form').areYouSure( {'message':'You have unsaved changes on this page. Do you want to leave this page and discard your changes or stay on this page?'} );

最佳答案

删除 turbolinks 通过删除//= 需要 application.js 文件中的 turbolinks

(目前为 2018 年 1 月)

jquery.AreYouSure 仅在 turbolinks 处于事件状态时刷新页面时才有效。

关于javascript - jQuery.AreYouSure 插件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com