IE10 – JQuery ajax html没有加载数据

我已经通过查看以下post( Ajax不能在IE10上工作 )和其他一些网站做了一些研究,但遗憾的是它没有解决我的问题,这就是我需要你帮助的原因;)

下面你可以看到我的JQuery ajax脚本,我的问题是我可以获取ajax并将值返回到errorDivStyle,但是我无法加载$(“#mainView”)。html(data); 数据从PHP脚本及其HTML返回。

请注意它在Chrome,FF和IE 9中完全正常,但不是IE10。 有什么我做错的吗?

 $(document).ready(function () { var category = $("#category").val(); $("#category").change(function(){ $.ajax({ type: "POST", url: "test.php", data: $(this).serialize(), cache: false, success: function(data) { if(data) { $("#mainView").html(data); $("#errorDivStyle").html("" + category + " category have been loaded").show(); } } }); return false; }); });  

我看到你的代码没有任何明显的错误。

如果您使用的是较新版本的Jquery,那么我建议您更改:

 $("#category").change(function(){ 

 $("#category").on('change', function(){ 

当然之后

 cache: false, 

 dataType: 'html', 

另外我强烈建议使用console.log()

 if(data) { //check if expected data is correct console.log(data); //make sure you can target #mainview properly //should output the amount of #mainviews on your screen console.log($("#mainView").length); $("#mainView").html(data); $("#errorDivStyle").html("" + category + " category have been loaded").show(); } 

编辑:

  $("#category").on('change', function(){ e.preventDefault(); $.ajax({ type: "POST", url: "test.php", data: $(this).serialize(), cache: false, dataType: 'html', success: function(data) { if(data) { $("#mainView").html(data); $("#errorDivStyle").html("" + category + " category have been loaded").show(); } } }); }); 

你为什么还要返回FALSE? 我刚刚意识到这可能是你正在倾听的文本字段更改。 所以e.preventDefault()也不是必需的……

此外,如果您只想将单个值传递给test.php,那么您可以尝试这样做:

  $("#category").on('change', function(){ $.ajax({ url: "test.php?q="+$(this).val(), cache: false, dataType: 'html', success: function(data) { if(data) { $("#mainView").html(data); $("#errorDivStyle").html("" + category + " category have been loaded").show(); } } }); }); 

编辑:递归TRIM一个数组

 /** * Returns a recursively TRIMMED variable * * @access public * @param mixed * @return mixed */ if ( ! function_exists('trim_r')) { function trim_r($var) { //In here you can add more code to handle objects properly but I did not because I am not using them with this function //if (is_array($var) || is_object($var)){some code} if (is_array($var)) { return array_map('trim_r', $var); } else { return trim($var); } } } // Usage examples $_POST = trim_r($_POST); $_GET = trim_r($_GET); $_REQUEST = trim_r($_REQUEST); $custom_array = trim_r($custom_array);