为什么我不'在ajax发布请求后无法获取数据

Why i don't get data after ajax posts requests?

本文关键字:请求 数据 获取 布请求 ajax 为什么      更新时间:2023-09-26

为什么AJAX发布请求后我不获取数据?

当我使用时

$('#fid1 input,select').attr('disabled','disbaled');

用于禁用ajax发送后处理之间的表单输入

并使用

$('#fid1 input,select').removeAttr('disabled');

用于在ajax发送成功后启用表单输入

index.php

<form method="post" id="fid1">
    <input type="checkbox" id="check_box_one" name="color_check" value="one" onclick="sedn_ajax()">1<br>
    <input type="checkbox" id="check_box_two" name="color_check" value="two" onclick="sedn_ajax()">2<br>
</form>
<script>
function sedn_ajax(){
    $('#fid1 input,select').attr('disabled','disbaled');
    $('#demoajax').hide();
    $('#loading').show();
    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: $('#fid1').serialize(),
        success: function(data){
            $("#loading").fadeOut("slow");
            $('#demoajax').show();
            $('#demoajax').html(data);
            $('#fid1 input,select').removeAttr('disabled');
            }
        });
    return false;
}
$(document).ready(sedn_ajax());
</script>

test.php

<?PHP echo $_POST['color_check']; ?>

您有多个问题

function sedn_ajax(){
    $('#fid1 input,select').attr('disabled','disbaled');  //spelled disbaled wrong - lucky it is truthy. You also are slecting all selects in the page, not just the elements in the form. 
    $('#demoajax').hide();
    $('#loading').show();
    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: $('#fid1').serialize(),  //serialize does not read disabled inputs
        success: function(data){
            $("#loading").fadeOut("slow");
            $('#demoajax').show();
            $('#demoajax').html(data);
            $('#fid1 input,select').removeAttr('disabled');
            }
        });
    return false;
}
$(document).ready(sedn_ajax());  //WRONG should not be ()

随着的变化

function sedn_ajax(){
    var data = $('#fid1').serialize(); //read the form values before disabled
    $("#fid1").find('input,select').prop('disabled', true);  //set it to true and use prop, not attr
    $('#demoajax').hide();
    $('#loading').show();
    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: data,  //use the variable
        success: function(data){
            $("#loading").fadeOut("slow");
            $('#demoajax').show();
            $('#demoajax').html(data);
            $("#fid1").find('input,select').prop('disabled', false);   //set it to false and use prop()
            }
        });
    return false;
}
$(document).ready(sedn_ajax);  //No ()

以及如何改进,将查找结果存储到变量中,而不继续查找DOM中的元素。