doujin4031 2015-12-25 14:30
浏览 61
已采纳

在AJAX中成功后重新启用“提交”按钮

I have been trying to enable Submit Button after the AJAX Request success. The validation is in PHP file which has following code

    if(!empty($_POST["username"])) {

        $fetch = "SELECT * FROM student WHERE std_username='" . $_POST["username"] . "'";
        $result = mysqli_query($con, $fetch);
        $user_count = mysqli_num_rows($result);

        if($user_count>0) {
            echo "<span class='status-not-available'> Username Not Available.</span>";
        }else{
            echo "<span class='status-available'> Username Available.</span>";
        }
    }

and this is AJAX

    function checkAvailability() {
        $("#loaderIcon").show();
        jQuery.ajax({
        url: "check_availability.php",
        data:'username='+$("#username").val(),
        type: "POST",
        success:function(data){
            $("#user-availability-status").html(data);
            $("#loaderIcon").hide();
        },
        error:function (){}
        });
    }

I have used this is success what it does is disable the submit button even the data matches the request or not, the button stays disabled

    $("#submit").attr("disabled", true);

and following in error

    $("#submit").attr("disabled", false);

I have gone throught these questions and tried the solutions but those solutions didn't work out for me.

jQuery: Re-enabling disabled input buttons after 'success:' is run and

Disable Button while AJAX Request

this is the HTML

    <form method="POST">
        <label>Check Username:</label>
        <input name="username" type="text" id="username" class="demoInputBox" onBlur="checkAvailability()"><span id="user-availability-status"></span><br>    
        <label>Check Username:</label>
        <input name="email" type="text" id="email" class="demoInputBox" onBlur="checkAvailabilitye()"><span id="email-availability-status"></span> <br>
        <input type="submit" id="submit"> 
    </form>

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duangejian6657 2015-12-25 14:36
    关注

    on the success event, you need to set the disabled property value to false. This will re-enable the button

    $( "#submit" ).prop( "disabled", true ); //disable when ajax call starts
    
    jQuery.ajax({
        url: "check_availability.php",
        data:'username='+$("#username").val(),
        type: "POST",
        success:function(data){
            $( "#submit" ).prop( "disabled", false ); //enable it back
            $("#user-availability-status").html(data);
            $("#loaderIcon").hide();
        },
        error:function (){}
        });
    

    EDIT : As per the comments,

    Looks like you are trying to enable/disable button based on the check you are doing aginist the db table. Currently your php script is returning a human readable string. May be you can change your php script to return a flag value which your javascript code can use.

    if($user_count>0) {
            echo "no";
    }else{
            echo "available";
    }
    

    And in your success handler inspect this value and hide/show buttons/messages

    success:function(data){
            $("#loaderIcon").hide();
            var msg="Not available";
    
            if(data=="available")
            {
              $( "#submit" ).prop( "disabled", false ); //enable it back
              msg="Username available";
            }
            $("#user-availability-status").html(msg);
    
    },
    

    I am not a PHP person. There could be a way to return JSON structure from your PHP page which can return both the flag and the message you want to show in the message div. You may try that too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类