为什么我在ajax发布请求后没有得到数据?

时间:2014-09-11 04:03:59

标签: javascript php jquery ajax

为什么我在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']; ?>

1 个答案:

答案 0 :(得分:1)

您有多个问题

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中的元素。