Sure, here's an example of how you can achieve this using jQuery:

<input type="text" id="input" />

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  let ajaxRequest = null;

  $('#input').on('input', function() {
    const inputValue = $(this).val();

    // Check if input length is equal or greater than 3
    if (inputValue.length >= 3) {
      // Abort the previous ajax request, if it exists
      if (ajaxRequest) {
        ajaxRequest.abort();
      }
      
      // Make a new ajax request
      ajaxRequest = $.ajax({
        url: 'your_api_endpoint',
        method: 'POST',
        data: { input: inputValue },
        success: function(response) {
          // Handle the successful response
          console.log(response);
        },
        error: function(error) {
          // Handle the error
          console.log(error);
        }
      });
    }
  });
</script>

In this example, we listen for the input event on the input field. Whenever the user types or deletes a character, the event will be triggered. We then retrieve the value of the input field using $(this).val(). We check if the length of the input value is equal to or greater than 3.

If the condition is met, we first abort any ongoing ajax request using ajaxRequest.abort() to stop the previous request (if any). Then, we make a new ajax request using $.ajax(). You need to replace 'your_api_endpoint' with the actual endpoint URL to which you want to make the request.

The success callback function handles the successful response from the server, and the error callback function handles any errors that may occur during the ajax request.

Note that you should replace 'your_api_endpoint' with the actual URL of your API endpoint, and handle the response and error accordingly

i want to check and run event when input type a letter and letter length is equal or greator that 3 letters in jquery pleasei want an ajax call and if user type new letter the last ajax call stopped

原文地址: http://www.cveoy.top/t/topic/iosM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录