Javascript 动态填充下拉列表的Ajax调用适用于FF,但不适用于Chrome和IE
我真的不知道为什么我的代码能在Firefox中完美运行,但当我在Chrome或IE中测试它时,它并没有填充动态下拉列表。我读过一些类似的帖子,但大多数人说这是因为我没有一个未关闭的div 代码如下:Javascript 动态填充下拉列表的Ajax调用适用于FF,但不适用于Chrome和IE,javascript,html,internet-explorer,google-chrome,Javascript,Html,Internet Explorer,Google Chrome,我真的不知道为什么我的代码能在Firefox中完美运行,但当我在Chrome或IE中测试它时,它并没有填充动态下拉列表。我读过一些类似的帖子,但大多数人说这是因为我没有一个未关闭的div 代码如下: <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"/>
</head>
<body>
<input type="textbox" name= "tag" id="tags">
<p>
<select id="movieImdbId" name="movieImdbId[]" multiple="multiple" width="200px" size="10px" style=display:none;>
</select>
</p>
<script type="text/javascript">
$(document).ready(function () {
$("#tags").autocomplete({
source: "actorsauto.php", //php file which fetch actors name from DB
minLength: 2,
select: function (event, ui){
var selectedVal = $(this).val();
// Here goes your ajax call.
$.post("actions.php", {q: selectedVal}, function (response){
// response variable above will contain the option tags.
$("#movieImdbId").html(response).show();
});
}
});
});
</script>
</body>
</html>
尝试显式映射您的响应这里有一个示例,来自我的代码更改以适用 从post请求返回一个json对象,包含两个值:标签、要从自动完成中显示的内容以及要与该标签关联的值。在本例中,我将该值保存在property.url中作为值,因为我希望自动完成与url同步 响应函数将结果绑定到控件。它接受将数组映射到所需结构的jquery委托。标签映射到templab item.label我从post中用json序列化的属性 值映射到item.url 我不确定这是否能解决您的问题,但现在您将显式绑定数据
success: function (data) {
response($.map(data, function (item) {
// this line below is a hack to get out the html encoded entities (> <...)
var templab = $('#searchhtmlconverter').html(item.label).text();
return { label: templab, value: item.url };
}))
多亏了,我终于可以通过使用而不是更改事件来解决我的问题
这就是为我工作的代码:
$(document).ready(function () {
$("#tags").autocomplete({
source: "actorsauto.php",
minLength: 2,
select: function (event, ui){
$("#tags").on('autocompletechange change', function (){
var selectedVal = $(this).val(); //this will be your selected value from autocomplete
$.post("actions.php", {q: selectedVal}, function (response){
$("#movieName").html(response).show();
});
}).change();
}
});
}) Chrome和IE中的开发工具有错误吗?响应的示例值是多少?@AnthonyGrist:我不确定我是否理解你的意思,当我在chrome中按F12时,它在控制台中显示此错误:加载资源失败:服务器响应状态为404未找到,获取http://localhost/res/images/ui-bg_glass_75_dadada_1x400.png 404未找到开发工具显示各种错误,在这种情况下,包括尝试加载由于服务器关闭、URL不正确等原因而无法加载的图像资源。在打开开发人员工具的情况下,从autocomplete运行AJAX调用,看看您得到了什么;在Chrome中,你想查看“网络”选项卡。@AnthonyGrist:thnaks,但我不明白它是否有错误为什么在firefox中工作?如果有错误,他们会怎么说?这也许可以解释为什么它在Firefox中工作,但在其他浏览器中不工作,或者可能不工作。谢谢你的回答,对不起,我不擅长javascript,你能解释一下你的代码吗?我不明白我到底需要在自己的代码中更改或添加什么,谢谢