2014-09-11 106 views
-1

为什么我在AJAX帖子请求后没有得到数据?为什么我在ajax帖子请求后没有获取数据?

当我使用

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

用于禁用AJAX发送POST过程之间形式输入

并使用

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

用于使AJAX发送POST成功后形式输入

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']; ?> 
+3

禁用的元素不包含在表单请求中或从'.serialize()'生成的数据中。 – 2014-09-11 04:06:00

+0

如何禁用表单并将数据发送到test.php – 2014-09-11 04:06:51

+1

将表单序列化,然后禁用输入,如果这是您的目标。 – 2014-09-11 04:08:03

回答

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() 

以及它如何能够得到提高,存储look- UPS 到变量中,并且不要继续查找DOM中的元素。

相关问题