jQuery Ajax:复制 - 粘贴东西

发布于 2024-09-01 10:27:46 字数 269 浏览 1 评论 0原文

我有一个表单,其中检查输入字段的值,然后使用 typewatch 插件(一个小东西,检测用户在预定义的时间间隔后停止输入)进行 Ajax 调用。效果很好。

由于此字段是订单中的“优惠券折扣”,因此可以通过从电子邮件或其他内容复制粘贴“优惠券代码”来输入该值。它还可以与 Ctrl+C - Ctrl+V 一起使用来进行复制和粘贴,但当用户用鼠标选择文本、从上下文菜单中单击复制,然后从此菜单中粘贴时,则不能使用。

jQuery 中有没有一种方法可以以某种方式检查 jQuery 中的这种行为?

I have a form where a check an input field for its value and then i do an Ajax call using the typewatch plugin ( a small little thing which detects that the user has stopped typing after a predefined interval ). It works great.

As this field is a "coupon discount" in an order form, the value could be entered by copy pasting the "coupon code" from an email or something. It also works with Ctrl+C - Ctrl+V for Copy and paste but not when a user selects the text with the mouse, clicks copy from the context menu and then paste from this menu.

Is there a way in jQuery to check for this kind of behaviour in jQuery somehow?

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

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

发布评论

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

评论(3

橘香 2024-09-08 10:27:46

您可以设置一个较短的超时(1 秒)来检查自上次检查以来值是否已更改。

您也可以简单地忽略它,当表单提交时,获取此优惠券折扣输入的内容并进行处理。

You could set a low time out (1 second) that checks is the value has changed since the last check.

You could also simply ignore it and when the form submits grab the contents of this coupon discount input and process it.

只是在用心讲痛 2024-09-08 10:27:46

我以前也遇到过类似的问题。据我所知,当用户通过右键单击粘贴或从以前使用的值列表中进行选择时,浏览器不会发出任何事件。

我最终通过将事件处理程序绑定到模糊事件以足够好的方式解决了这个问题。唯一没有捕获的情况是当他们用鼠标粘贴一个值,然后按 Enter 键提交时。我能够通过一些技巧来解决这个问题:

$(element)
  .bind('keydown blur', handlerFunction)
  .keydown(function(event) {
    if (event.keyCode === 13 /* Enter Key */) {
      event.preventDefault();
      event.stopPropagation();
    }
  })
  .keyup(function(event) {
    if (event.keyCode === 13 /* Enter Key */) {
      $(this).closest('form').submit();
    }
  });

本质上,这是有效的,因为 jQuery 将按照事件绑定的顺序执行事件。通常,表单提交是在按下 Enter 按键时进行的。为了让 keyup 处理程序有时间发挥其魔力,您需要延迟表单提交,直到 keyup 事件发生,以便其他 keyup 处理程序可以先运行。

现在,您说您正在使用一个插件,该插件会在对用户事件做出反应之前添加延迟。这意味着您可能无法逐字使用此技术。可能有效的一件事是在允许表单提交之前直接调用处理函数。为此,您可以将最后一个 keyup 处理程序更改为:

  .keyup(function(event) { // Part 2
    if (event.keyCode === 13 /* Enter Key */) {

      handlerFunction.call(this, event); // force it to happen now

      $(this).closest('form').submit();
    }
  });

正如 Josh K 之前提到的,您可以简单地将某些内容绑定到表单上的提交事件来执行类似的操作。不过,如果您在那里执行 Ajax,请务必在传递的选项中设置 async: false,否则表单将继续并提交,而无需等待调用完成。

I have had a similar issue before. As far as I know, the browser issues no events when the user pastes via right click, nor when they select from the list of previously used values.

I ended up solving it in a good enough way by additionally binding my event handler to the blur event. The only case this didn't catch was when they paste a value with the mouse, then hit the Enter key to submit. I was able to get around this through a little hackery:

$(element)
  .bind('keydown blur', handlerFunction)
  .keydown(function(event) {
    if (event.keyCode === 13 /* Enter Key */) {
      event.preventDefault();
      event.stopPropagation();
    }
  })
  .keyup(function(event) {
    if (event.keyCode === 13 /* Enter Key */) {
      $(this).closest('form').submit();
    }
  });

Essentially, this works because jQuery will execute the events in the order they were bound. Normally, form submission takes place on the keydown of an Enter keypress. In order to give the keyup handler time to do its magic, you need to delay that form submission until the keyup event happens, so the other keyup handler can run first.

Now, you said that you were using a plugin that adds a delay before reacting to the users event. That means you may not be able to use this technique verbatim. One thing that might work instead would be to call the handler function directly before allowing the form to submit. To do that, you would change the last keyup handler to:

  .keyup(function(event) { // Part 2
    if (event.keyCode === 13 /* Enter Key */) {

      handlerFunction.call(this, event); // force it to happen now

      $(this).closest('form').submit();
    }
  });

As Josh K mentioned before, you could simply bind something to the submit event on your form to do something similar. If you are doing Ajax there, though, be sure to set async: false in the passed options, because otherwise the form will go ahead and submit without waiting for the call to complete.

不打扰别人 2024-09-08 10:27:46

(这个解决方案还没有经过测试,但是!)我会尝试这样的事情:

var keys_pressed = 0; // count the number of key press events
var required_keys_pressed = 10;
$('#yourinputElement').keydown ( function () {
  keys_pressed++; // add one
}); 
$('#yourform').submit (function () {
  if (keys_pressed < required_keys_pressed)
  {
     // not enough keys pressed or was a CTRL + v
  }
  else
  {
     // required number of keys pressed for your code
  }
});

记住人们总是可以使用浏览器插件绕过这个,所以这并不能保证事情按照你想要的方式进行他们到。您也应该始终执行某种服务器端验证

(this solution has not been tested, but!) I'd try something like this:

var keys_pressed = 0; // count the number of key press events
var required_keys_pressed = 10;
$('#yourinputElement').keydown ( function () {
  keys_pressed++; // add one
}); 
$('#yourform').submit (function () {
  if (keys_pressed < required_keys_pressed)
  {
     // not enough keys pressed or was a CTRL + v
  }
  else
  {
     // required number of keys pressed for your code
  }
});

Remember though people can always bypass this with browser plugins, so it's not a guarantee that things have gone the way you want them to. You should ALWAYS perform some kind of server-side validation too

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