简体   繁体   English

如何使用jQuery检查两个输入字段是否填写并等待2秒

[英]How to check if two input fields are filled in and wait 2 seconds using jquery

I am using an api that is getting the correct street name and place when a user types in his zip code and house number. 当用户输入邮政编码和门牌号时,我使用的API会获得正确的街道名称和位置。

Currently I got the following layout: 目前我有以下布局:

<p class="form-row form-row form-row-wide address-field validate-required" id="billing_postcode_field" data-o_class="form-row form-row form-row-last address-field validate-required validate-postcode">
   <label for="billing_postcode" class="">Postcode<abbr class="required" title="required">*</abbr></label><input type="text" class="input-text " name="billing_postcode" id="billing_postcode" value="">
</p>
<p class="form-row form-row form-row-wide address-field validate-required" id="billing_housenumber_field" data-o_class="form-row form-row form-row-last address-field validate-required validate-postcode">
   <label for="billing_housenumber" class="">Huisnummer<abbr class="required" title="required">*</abbr></label><input type="text" class="input-text " name="billing_housenumber" id="billing_housenumber" value="">
</p>
<p class="form-row form-row form-row-wide address-field validate-required" id="billing_address_1_field">
   <label for="billing_address_1" class="">Adres<abbr class="required" title="required">*</abbr></label><input type="text" class="input-text " name="billing_address_1" id="billing_address" value="">
</p>
<p class="form-row form-row form-row-wide address-field validate-required" id="billing_city_field" data-o_class="form-row form-row form-row-wide address-field validate-required">
   <label for="billing_city" class="">Plaats<abbr class="required" title="required">*</abbr></label><input type="text" class="input-text " name="billing_city" id="billing_city" placeholder="" value="">
</p>

Inside a form with id checkoutform . 在ID为checkoutform的表单内。

I've tried the following already: 我已经尝试了以下方法:

tpj('#checkoutform').on('input', function() {
  var val1 = tpj('#billing_postcode').val();
  var val2 = tpj('#billing_housenumber').val();
  if (val1 > 0 && val2 > 0) {
    console.log('both filled in');
  } else {
    console.log('both not filled in');
  }
});

This shows the log in the console every keypress inside either fields. 这将在两个字段中的每个按键上显示控制台中的日志。 In the end I want to post with ajax to a PHP script that makes an api call, I got a limited amount of calls each day so this is very bad. 最后,我想用ajax发布到进行api调用的PHP脚本,每天我得到的调用数量有限,所以这非常糟糕。

I only want to make 1 request when both fields are filled in, what would be the best course of action? 当我同时填写两个字段时,我只想发出1个请求,最好的做法是什么?

I was thinking maybe wait 1 or 2 seconds after both fields are filled in and then post to the script. 我当时想在两个字段都填写后等待1或2秒钟,然后发布到脚本中。

How can I do that? 我怎样才能做到这一点? (Only the time part, I can fix the ajax posting myself). (只有时间部分,我可以自己修复ajax)。

If anyone has a better suggestion to what I am thinking please say so. 如果有人对我的想法有更好的建议,请这样说。

Here is one approach: 这是一种方法:

  1. Set a timeout function for 2000ms and in it check if both inputs have values. 将超时功能设置为2000ms,并在其中检查两个输入是否都具有值。 1.1. 1.1。 If they have values, post the form. 如果它们具有值,请过帐表格。 1.2. 1.2。 If they don't, set the timeout function again. 如果没有,请重新设置超时功能。
  2. On an input event clear the timeout and set it again for 2000ms - this will reset your timeout counter. 在输入事件中,清除超时并重新设置2000ms-这将重置您的超时计数器。

All this will guarantee that you will always wait for at least two seconds after each input and also will continue waiting till you have something in both of your inputs. 所有这些将确保您每次输入后始终等待至少两秒钟,并且还将继续等待直到两个输入中都包含内容。

You dont need to wait 2 seconds, just make the event when the user fill the form. 您无需等待2秒钟,只需在用户填写表格时进行该事件即可。

$(function()
{
    $("#btn").on("click",function()
    {
        let inputOne = $("#inputOne").val();
        let inputTwo = $("#inputTwo").val();
        if (inputOne == "" || inputTwo == "")
        {
            alert("fill inputs");
        }
        else
        {
            // Ajax call goes here
        }
    })
})

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

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