仅当输入两个或更多字符时才触发AJAX

时间:2019-11-11 20:05:41

标签: javascript ajax

我有以下代码:

<script>
    function pesquisa_cid_01() {
        var oHTTPRequest = createXMLHTTP(); 
        oHTTPRequest.open("post", "bd_ajax_ciat_cid.asp", true);
        oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        oHTTPRequest.onreadystatechange=function() {
            if (oHTTPRequest.readyState == 4) {
                document.all.div_pesquisa_cid_01.innerHTML = oHTTPRequest.responseText;
            }
        }
        oHTTPRequest.send("busca_cid_01=" + encodeURIComponent(form_ciat_new.pesquisa_cid_01_form.value));
    }
</script>

如何强制代码开始只能使用在表单输入中键入的2个加字符?

表单代码为:

<input type="text" name="pesquisa_cid_01_form" id="pesquisa_cid_01_form" class="form-control" onkeyup="pesquisa_cid_01();">

1 个答案:

答案 0 :(得分:2)

只需检查值的长度,如果不正确则退出。

function pesquisa_cid_01(e) {
   if (e.currentTarget.value.length < 2) {
     return;
   }

   ...
}
相关问题