skip to Main Content

I have two input text box and I have added the same key up event handler which calls the ajax request. I want to perform some snippet(enable the text box) on the “complete” event but they are not working independently.

<input type="text" name="ele1" id="ele1">
<input type="text" name="ele2" id="ele2">

$(function() {
    let ele1 = $('#ele1');
    let ele2 = $('#ele2');
    $("#ele1, #ele2").on('keyup', resourceChange);
    function resourceChange(e) {
        $this = $(this);
        $this.prop('disabled', true);
        $.ajax({
            url: "test_upload.php",
            type: "POST",
            cache: false,
            processData: false,
            contentType: false,
            dataType: 'json',
            complete: function(xhr) {
                response = xhr.responseJSON;
                $this.prop('disabled', false);
            }
        });
    }
});

for testing purposes, I manually add 5 seconds delay.

<?php
// test_upload.php
sleep(5);
$response = [
    'success' => true,
    'name' => 'test_name'
];
header('Content-Type: application/json');
echo json_encode($response);

Problem: When I type a character on the first text box, it will be disabled for the next five seconds(count down start). But If I type on second text box before complete these 5 seconds(let say., 3 seconds pass) then the first text box is never enabled. It remains to disable. and the second text box is enabled after remaining seconds from 5 seconds. (5 – 3 = 2 seconds)

2

Answers


  1. You should capture the response. And then decide what you need to do.

    Try this.

    <input type="text" name="ele1" id="ele1">
    <input type="text" name="ele2" id="ele2">
    <input type="text" name="ele3" id="ele3">
    <input type="text" name="ele4" id="ele4">
    

    $(function() {
            var respCount = 0;
            var selectedElem = [];
            let ele1 = $('#ele1');
            let ele2 = $('#ele2');
            let ele3 = $('#ele3');
            let ele4 = $('#ele4');
    
            var idx = 0;
            $("#ele1, #ele2, #ele3, #ele4").on('keyup', resourceChange);
            function resourceChange(e) {
    
                $this = $(this);
                selectedElem.push({id: $this[0].id});
    
                $this.prop('disabled', true);
                $.ajax({
                    url: "test_upload.php",
                    type: "POST",
                    cache: false,
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    complete: function(xhr) {
                        response = xhr.responseJSON;
    
                        $(`#${selectedElem[respCount].id}`).prop('disabled', false);
    
                        respCount++;
    
                    }
                });
    
    
            }
    
    
    
        });
    
    Login or Signup to reply.
  2. Try this one by sending element via ajax and recive it.

    <input type="text" name="ele1" id="ele1">
    <input type="text" name="ele2" id="ele2">
    
    $(function() {
    $("input").on('keyup', resourceChange);
    function resourceChange(e) {
        element = $(this).attr('name');
        $(this).prop('disabled', true);
        $.ajax({
            url: "test_upload.php",
            type: "POST",
            cache: false,
            processData: false,
            contentType: false,
            data: 'element='+element,
            dataType: 'json',
            complete: function(xhr) {
                response = xhr.responseJSON;
                $('input[name="'+response.element+'"]').prop('disabled', false);
            }
        });
    }
    });
    

    php

     <?php
    // test_upload.php
    sleep(5);
    $response = [
      'success' => true,
      'name' => 'test_name',
      'element' => $_POST['element']
    ];
    header('Content-Type: application/json');
    echo json_encode($response);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search