How do you send an ajax request every time that a form input field changes?

前端 未结 4 1474
花落未央
花落未央 2021-02-09 05:34

For example, there is an input field. Every time a user types a key into that field, it sends an AJAX request with whatever text is currently in that input, and does something

4条回答
  •  旧巷少年郎
    2021-02-09 06:03

    sending a request on each change is just bad, delay the ajax on the last change

    var changeTimer = false;
    
    $("your inputs").on("your change event",function(){
            if(changeTimer !== false) clearTimeout(changeTimer);
            changeTimer = setTimeout(function(){
                /* your ajax here */
                changeTimer = false;
            },300);
    });
    

提交回复
热议问题