jQuery - 结合“确认”和“someOtherAction”

发布于 2024-09-30 02:19:42 字数 1273 浏览 0 评论 0原文

我有以下代码,它仅使用 jquery ui 模式对话框来“确认”链接上的任何点击。如果他们单击“确定”,则会将他们发送到原始链接。它工作得很好,所以不必费心阅读太多细节 =)

    $('.confirm').live('click', function(e) {
  e.preventDefault();
  var theHref = $(this).attr('href');
  var confirmText = 'Are you sure?';
  if ($(this).attr('confirm')) {
   confirmText = $(this).attr('confirm');
  }
  confirmDialog.html(confirmText);
  confirmDialog.dialog({
   buttons: {
    'OK': function() {
     window.location = theHref;
     $(this).dialog('close');
    },
    Cancel: function() {
     $(this).dialog('close');
     return false;
    }
   }
  });
  confirmDialog.dialog('open');
  return false;
 });

然后,我还有一堆其他任意功能。就像这样说:

    $('a.reset').live('click', function(e) {
  e.preventDefault();
  $(this).parents('form')[0].reset();
 });

这只是一个重置表单的链接(如输入类型=重置)。这个功能也能正常工作。请注意,这只是一个任意示例。我通过类附加了许多不同的 JS 功能片段。

我遇到的问题是将这两个堆叠起来。在此示例中,某些重置链接我可能需要确认,有些我可能不需要。就像这样:

<a href="" class="reset confirm">Reset on confirmation</a>
<a href="" class="reset">Reset without confirmation</a>

当然,问题是第一个示例不关心确认 - 它只是继续并重置。有什么方法可以继续保持这些代码的独立和模块化(就像在 HTML 示例中一样),同时使一个代码依赖于另一个的输出?

我希望我想要实现的目标很清楚。

预先感谢您的任何帮助。第一次在这里发帖,请多多关照=)

I have the following code that simply uses a jquery ui modal dialog to "confirm" any click on a link. If they click OK, then it sends them to the original link. It works fine, so don't bother reading it in too much detail =)

    $('.confirm').live('click', function(e) {
  e.preventDefault();
  var theHref = $(this).attr('href');
  var confirmText = 'Are you sure?';
  if ($(this).attr('confirm')) {
   confirmText = $(this).attr('confirm');
  }
  confirmDialog.html(confirmText);
  confirmDialog.dialog({
   buttons: {
    'OK': function() {
     window.location = theHref;
     $(this).dialog('close');
    },
    Cancel: function() {
     $(this).dialog('close');
     return false;
    }
   }
  });
  confirmDialog.dialog('open');
  return false;
 });

Then, I have a bunch of other arbitrary functionality. Like, say this:

    $('a.reset').live('click', function(e) {
  e.preventDefault();
  $(this).parents('form')[0].reset();
 });

That's just a link that resets a form (like an input type = reset). This functionality also works fine. Please note this is just an arbitrary example. I've got plenty of different snippets of JS functionality that I attach via classes.

The problem I am encountering is stacking these two. In this example, some reset links I might want confirmations, some I might not. Like so:

<a href="" class="reset confirm">Reset on confirmation</a>
<a href="" class="reset">Reset without confirmation</a>

The problem of course, is that the first example doesn't care about the confirm - it just goes ahead and resets. Is there some way I can continue to keep these bits of code separate and modular (like in the HTML example), while at the same time making one dependent on the output of the other?

I hope it is clear what I'm trying to achieve.

Thanks in advance for any assistance. First time posting on here so be nice =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的呼吸 2024-10-07 02:19:42

我不知道这是否有效,但尝试以相反的顺序创建 .live 处理程序。我认为它们可能会从最后一个附加到第一个附加触发。

I don't know if this will work, but try creating the .live handlers in reverse order. I think they might get triggered from last attached to first attached.

浪推晚风 2024-10-07 02:19:42

好吧,根据我的理解,你想要实现的一种非常简单的方法是在阻止任何默认行为之前检查该元素是否附加了确认类,以及不阻止什么行为

well one very simple approach ill guess from what i understood you want to achieve would be to check whether the element has the confirmation class attached to it before you prevent any default behaviour and what not

夏至、离别 2024-10-07 02:19:42

我认为您可以通过简单地将代码附加到重置类之前的确认类来解决这个问题。只需将其放在 document.ready 的较高位置或您指定事件的任何位置即可。

I think you can probably sort this out by simply attaching the code to the confirm class before the reset class. Just put it higher up in your document.ready or wherever you specify the events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文