submit form with delay while entering data into input field

后端 未结 2 1496
暖寄归人
暖寄归人 2021-02-14 14:51

I have a simple form that I need to submit automatically when text is entered.

I can use the onChange or onKeyUp without the best result.

HTML is:



        
相关标签:
2条回答
  • 2021-02-14 15:08

    This should work. Submits the form when nothing was typed for 500ms

    var timerid;
    jQuery("#fusionSearchForm").keyup(function() {
      var form = this;
      clearTimeout(timerid);
      timerid = setTimeout(function() { form.submit(); }, 500);
    });
    
    0 讨论(0)
  • 2021-02-14 15:24

    This is a bad idea. You don't want to circumvent the controls people expect on form inputs. If you don't want a submit button then at least wait to submit until you capture an "enter".

    Other than a user mind reading device how could you ever know when a user is done typing, unless you were looking for them to type "return" or "enter"? Users won't know they're supposed to pause a moment to let the form submit at the timeout, or that they need to hurry up to get their search entered before the timeout of 500MS.

    See: this question

    Of if you want to start returning results immediately upon any submission you could do some kind of ajax autocomplete.

    autocomplete http://img9.imageshack.us/img9/5526/autocompletegoogle.png

    Here's a jQuery example of submitting on an enter:

      $("input").keypress(function(e) {
                switch (e.keyCode) {
                        case 13:
                                $("#formid").submit();
                                return false;
                        default:
                                return true;
                }
        });
    
    0 讨论(0)
提交回复
热议问题