简体   繁体   English

如何在JQuery中解决此问题?

[英]How do I solve this problem in JQuery?

$("#id_address_input").bind("blur",function(e){
    //do a lot of stuff, set variables.
});

So, I have a text box that when the user gets off of it...this thing gets fired and processes. 所以,我有一个文本框,当用户离开它时,该东西将被触发并处理。

Ok, the problem is this: 好的,问题是这样的:

When the user clicks the form Submit button, this blur thing gets fired. 当用户单击表单“提交”按钮时,将触发此模糊事件。 And in the middle of processing, the form submits. 并在处理过程中提交表单。 This messes everything up. 这弄乱了一切。

How do I fix this? 我该如何解决? I thought about adding a delay on form.submit, so that it'll finish processing. 我考虑过要在form.submit上添加一个延迟,以便它完成处理。 But, that's a bad idea because I haven idea how fast the user's browser and/or Google Maps is. 但是,这是一个坏主意,因为我还不知道用户的浏览器和/或Google Maps有多快。

Do your form submit using javascript. 您的表单是否使用javascript提交。 Write a function which calls on onsubmit and check to see if the things that were supposed to happen on blur event have finished. 编写一个调用onsubmit的函数,并检查应该在模糊事件中发生的事情是否已经完成。 Then only submit your form 然后只提交您的表格

Try a flag? 尝试标记?

var doSubmit = true;

$("#id_address_input").bind("blur",function(e){
    doSubmit = false;
    //do a lot of stuff, set variables.
    doSubmit = true;
});

$("#form").submit(function(){
    if(doSubmit){
        return true;
    }else{
        // keep checking until doSubmit is true
        window.setInterval(function(){if(doSubmit){$("#form").submit();}},1000);
        return false;
    }
});

In your blur handler, you must be doing something that "yields" to the browser (a setTimeout , an asynchronous ajax call, etc.), because otherwise, the browser will not interrupt your JavaScript code to submit the form. blur处理程序中,您必须执行“使”浏览器“屈服”的操作( setTimeout ,异步ajax调用等),因为否则,浏览器将不会中断您的JavaScript代码来提交表单。 See below for an example proving it. 请参见下面的示例证明。

If you are doing something that yields to the browser from within the blur handler, you'll have to prevent the form submission with a submit handler and then have it get submitted when everything (including the various actions in the blur handler) is ready. 如果您通过blur处理程序执行让浏览器屈服的操作,则必须阻止使用submit处理程序submit表单,然后在所有内容(包括blur处理程序中的各种操作)准备就绪时submit表单。 This may be a bit complicated. 这可能有点复杂。

The best answer is to remove whatever it is in the blur handler that's yielding to the browser. 最好的答案是删除blur处理程序中产生给浏览器的所有内容。 If you can't do that, you're probably looking at a locking counter on the form and having the submission handler check that locking counter: 如果您不能这样做,则可能是在查看表单上的锁定计数器,并让提交处理程序检查该锁定计数器:

// Somewhere, ensure the form has a lock count of zero
$('#theForm').data('submitlock', 0);

// Your blur handler increments the lock
// In the blur handler:
$('#theForm').data('submitlock', $('#theForm').data('submitlock') + 1);
// ...and when done
$('#theForm').data('submitlock', $('#theForm').data('submitlock') - 1);

// The submit handler might look something like this:
$('#theForm').submit(function() {

    // Get a jQuery wrapper for the form
    var $this = $(this);

    // Locked?
    if ($this.data('submitlock') > 0) {
        // Yes, set up the recheck and prevent form submission
        setTimeout(recheck, 10);
        return false;
    }

    // Function to periodically retry submission
    function recheck() {
        // Still locked?
        if ($this.data('submitlock') > 0) {
            // Keep waiting
            setTimeout(recheck, 10);
        }
        else {
            // Nope, submit the form using the underlying DOM object's submit function
            $this[0].submit();
        }
    }
});

Live example (a version of our blocking example below, but updated to yield to the browser) 实时示例 (以下阻止示例的版本,但已更新以使浏览器适应)

Beware that if your form is being submitted to a new window, you may run afoul of pop-up blockers (since the new window isn't in direct response to a user click). 请注意,如果将表单提交到新窗口,则可能会遇到弹出窗口阻止程序(因为新窗口并不直接响应用户单击)。 But if you're not doing something like that, you're in good shape with the above. 但是,如果您没有做类似的事情,那么上面的情况就很好。


Here's an example of a blur handler causing a substantial delay (four seconds) before the form is submitted ( live copy ): 这是一个blur处理程序的示例,该处理程序在提交表单( 实时复制 )之前导致相当长的延迟(四秒钟):

HTML: HTML:

<form action='http://www.google.com/search' target='_new'>
  <label>Search for:
    <input type='text' name='q' value='foo'></label>
  <br><input type='submit' value='Search'>
</form>

JavaScript: JavaScript的:

$('input[name=q]').blur(function() {

  var end, counter;

  display("Start blurring");
  counter = 0;
  end = new Date().getTime() + 4000;
  while (new Date().getTime() < end) {
    ++counter;
    if (counter % 100000 == 0) {
      display("Still blurring");
    }
  }
  display("Done blurring");

});

Put your cursor in the text box, and then click Search. 将光标放在文本框中,然后单击“搜索”。 You'll see a four-second delay, and then the search will open in a new window. 您会看到四秒钟的延迟,然后搜索将在新窗口中打开。 I've tested this on Chrome, Firefox, and Opera (on Linux), IE6 on Windows 2000, and IE7 on Windows XP. 我已经在Chrome,Firefox和Opera(在Linux上),Windows 2000上的IE6和Windows XP上的IE7上对此进行了测试。 All behaved the same way. 所有人的行为都一样。

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

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