繁体   English   中英

jQuery确认(外部插件)无法正常工作

[英]jQuery Confirm (external plugin) not working properly

我的网站上的jQuery确认代码一直存在一个非常奇怪的问题。 我一直在使用由Nadia Alramli设计的插件( http://nadiana.com/jquery-confirm-plugin

此插件会停止应用它的元素的操作,并首先提供确认对话框。 我在我开发的基金应用管理系统上使用它作为“动作按钮”。 一切都工作正常,直到最近,字面意思,突然之间,代码停止工作 确认对话框显示,但是当您单击“是”时,按钮的操作不会执行。

我检查并重新检查了代码,并没有从最初实现的方式改变。 我还创建了一个示例页面,我已经删除了所有其他不必要的代码,我仍然遇到同样的问题。 这是我的示例页面的链接: http//www.greeleyw2w.org/admin/example2.html

什么可能导致这样的事情? 我能想到的唯一的事情是我的网络主机(GoDaddy)的问题,或者谷歌托管的jQuery版本。

//Actions for the status change buttons
$(document).ready(function(){
$("#test").click(function(){ //The action of the button
    alert("You confirmed the button click.");
});

$('.confirm').confirm({ //Captures button action, asks for confirmation.
    msg:'Are you sure?',
    wrapper:'<span style="color:#099"></span>',
    dialogShow:'fadeIn',
    dialogSpeed:'slow',
    buttons: {
        wrapper:'<button></button>',
        separator:'  '
    }
});
});

它与JQuery 1.8不兼容。

在第43行附近,更改代码:

var events = jQuery.data(target,'events');

var events = jQuery._data(target,“events”);

由于删除了api,事件模型打破了1.8。

http://blog.jquery.com/2012/08/09/jquery-1-8-release

尝试更改您的代码

$('.confirm').confirm({
   ....
});

$('#test').confirm({
   ....
});

我想你错过了“是”按钮上的ID。 在这里你的代码:

<span style="color: rgb(0, 153, 153); display: none;">
Are you sure?
<button>Yes</button>
<button>No</button>
</span>
<input id="test" class="confirm" type="button" style="display: inline;" value="Test Confirm Function" />

所以通过将ID属性添加到Yes按钮来更改HTML代码。 看下面的修改后的代码:

<span style="color: rgb(0, 153, 153); display: none;">
Are you sure?
<button id="test">Yes</button>
<button>No</button>
</span>
<input class="confirm" type="button" style="display: inline;" value="Test Confirm Function" />

或者您可以将您的javascript操作部分更改为:

$('button').click(function(){
  alert("You confirmed the button click."); 
});

查看关于“第二个示例”的插件文档 ,它与您的情况类似。

在jquery上确认用以下代码替换代码:

/**
 * Confirm plugin 1.3
 *
 * Copyright (c) 2007 Nadia Alramli (http://nadiana.com/)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */

/**
 * For more docs and examples visit:
 * http://nadiana.com/jquery-confirm-plugin
 * For comments, suggestions or bug reporting,
 * email me at: http://nadiana.com/contact/
 */

jQuery.fn.confirm = function(options) {
  options = jQuery.extend({
    msg: 'Are you sure?',
    stopAfter: 'never',
    wrapper: '<span></span>',
    eventType: 'click',
    dialogShow: 'show',
    dialogSpeed: '',
    timeout: 0
  }, options);
  options.stopAfter = options.stopAfter.toLowerCase();
  if (!options.stopAfter in ['never', 'once', 'ok', 'cancel']) {
    options.stopAfter = 'never';
  }
  options.buttons = jQuery.extend({
    ok: 'Yes',
    cancel: 'No',
    wrapper:'<a href="#"></a>',
    separator: '/'
  }, options.buttons);

  // Shortcut to eventType.
  var type = options.eventType;

  return this.each(function() {
    var target = this;
    var $target = jQuery(target);
    var timer;
    var saveHandlers = function() {
      var events = jQuery.data(target, 'events');
      if (!events && target.href) {
        // No handlers but we have href
        $target.bind('click', function() {document.location = target.href});
        events = jQuery.data(target, 'events');
      } else if (!events) {
        // There are no handlers to save.
        return;
      }
      target._handlers = new Array();
      for (var i in events[type]) {
        target._handlers.push(events[type][i]);
      }
    }

    // Create ok button, and bind in to a click handler.
    var $ok = jQuery(options.buttons.wrapper)
      .append(options.buttons.ok)
      .click(function() {
      // Check if timeout is set.
      if (options.timeout != 0) {
        clearTimeout(timer);
      }
      $target.unbind(type, handler);
      $target.show();
      $dialog.hide();
      // Rebind the saved handlers.
      if (target._handlers != undefined) {
        jQuery.each(target._handlers, function() {
          $target.click(this.handler);
        });
      }
      // Trigger click event.
      $target.click();
      if (options.stopAfter != 'ok' && options.stopAfter != 'once') {
        $target.unbind(type);
        // Rebind the confirmation handler.
        $target.one(type, handler);
      }
      alert("You confirmed the button click.");
      return false;
    })

    var $cancel = jQuery(options.buttons.wrapper).append(options.buttons.cancel).click(function() {
      // Check if timeout is set.
      if (options.timeout != 0) {
        clearTimeout(timer);
      }
      if (options.stopAfter != 'cancel' && options.stopAfter != 'once') {
        $target.one(type, handler);
      }
      $target.show();
      $dialog.hide();
      return false;
    });

    if (options.buttons.cls) {
      $ok.addClass(options.buttons.cls);
      $cancel.addClass(options.buttons.cls);
    }

    var $dialog = jQuery(options.wrapper)
    .append(options.msg)
    .append($ok)
    .append(options.buttons.separator)
    .append($cancel);

    var handler = function() {
      jQuery(this).hide();

      // Do this check because of a jQuery bug
      if (options.dialogShow != 'show') {
        $dialog.hide();
      }

      $dialog.insertBefore(this);
      // Display the dialog.
      $dialog[options.dialogShow](options.dialogSpeed);
      if (options.timeout != 0) {
        // Set timeout
        clearTimeout(timer);
        timer = setTimeout(function() {$cancel.click(); $target.one(type, handler);}, options.timeout);
      }
      return false;
    };

    saveHandlers();
    $target.unbind(type);
    target._confirm = handler
    target._confirmEvent = type;
    $target.one(type, handler);
  });
}

暂无
暂无

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

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