- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这两天我绞尽脑汁,试图找到解决方案。我使用 jQuery.ajax() 从数据库中获取值,以便在另一个框发生更改时更新一个框。 php 脚本从数据库中获取值,然后输出 json。它在 FF 中工作正常,但在所有版本的 IE 中,选择框都不会更新。我已经确认输出的 json 是好的。
这是 jquery:
function getVendors(dest, selectSup) {
var vend = $('select#sup').val();
$.ajax({
beforeSend: function() {
$("select#dest").parent().addClass('loading');
},
type: "GET",
dataType: "json",
cache: false,
url: '/search/cruiseselect/?type=vendors&supplier=' + vend + '&dest=' + dest,
timeout: 2000,
error: function() {
alert("Failed to submit");
},
success: function(data) {
$("select#sup option").remove();
var any = "<option value=\"any\">-- All Cruise Lines --</option>";
$(any).appendTo("select#sup");
$.each(data, function(i, j) {
if (j != null && j != undefined) {
var sel = j.value == selectSup ? " selected" : "";
var row = "<option value=\"" + j.value + sel + ">" + j.text + "</option>";
//$(row).appendTo("select#sup");
$("select#sup").append(row);
}
});
},
complete: function() {
$("select#dest").parent().removeClass('loading');
}
});
}
jQuery(document).ready(function() {
//dynamic select boxes
$("select#dest").change(function() {
var selectSup = $("select#sup").children("option:selected").val();
getVendors($(this).val(), selectSup);
});
});
我的 php 中有这个
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($json);
它输出正确的 json,没有多余的逗号或任何东西。更重要的是,如果我使用alert(j.value + j.text);在我的 .each() 循环中,我在 IE 中获得了正确的数据,因此看起来 jquery appendTo() 不起作用。
有人有什么想法吗?
最佳答案
令我有点惊讶的是 jQuery 没有处理这个问题(我以为它可以处理......也许是 .html() 有效)。
该问题基于 IE (6,7,& 8) bug that you can't set the .innerHTML of a select list .
使用“vanilla”Javascript,您可以使用 Option 对象创建新选项并将它们添加到选择中,或者您可以一次设置整个选择列表(例如,包括选择标签)。
var mySelect = $("select#sup").get(0);//get actual DOM element
var newOpt,selLen;
for(var i=0;i<10;i++){
newOpt = new Option('Label: ' + i, i);
//format new Option(text, value, defaultSelected, selected);
//add new option to select object
selLen = mySelect.options.length;
mySelect.options[selLen] = newOpt;
//This may also work, but I don't recall if IE6 supports the .push()
//method on the options collection, if so, this line will replace the 2 above
// mySelect.options.push(newOpt);
}
关于javascript - jQuery appendTo(), json 在 IE 6,7,8 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1499246/
我认为这段代码应该可以工作,但它没有,有人可以解释一下吗? $("#addLinkLayout input.comment, #addLinkLayout input.link").each(fun
我有一个搜索框,并使用 Jquery 自动完成来获取我想要返回到自定义元素的搜索结果。 目前,每次搜索更改时它都会追加,从而导致重复。 我想知道是否有一个选项可以在搜索词更改时删除自定义元素的内容。
下面是我尝试过的两个代码: $( ""+ title +"" ).appendTo( "#sortable" ); $( "").html(title).appendTo( "#sortable" )
我有一个简单的 UL,其中所有 LI 都具有 class="event"。 以下 jQuery 未按预期工作。 $('#calendar-feed li.event').each(function()
确定相关 HTML: Cartridge Displays Cold Cast Bronze resin Figurines
我有一个变量,我想将其添加到其他文本中。我的第一个想法是: var wood = 5 $(wood+"bits of wood left" ).appendTo("#notifications");
我正在关注本教程 Drag Drop Inventory in Jquery 我遇到了问题。在教程附带的演示中,作者使用了以下代码: item.attr('src',drag_item.attr('s
我正在尝试做的(目前)是使用搜索字符串查询 Wikipedia API, $(document).ready(function() { $(".search").on("click", getAr
aaaaaa bbbbbb One Two Three ccc xxxxxx
我试图弄清楚是否可以使用 .appendTo() 将多个 jquery 对象添加到现有的 div 容器中。例如,我的代码示例目前类似于这样 var parentContainer = $(""),
我有这个 jquery 代码: function adaptMenu() { /* toggle menu on resize */ var screenWidth = (windo
我在 iframe 中有一个 php 文件,其中包含一个表单结果,我使用以下 jquery 代码在父文档中显示该结果。 在示例中,有 appendTo到 和一个输入元素。 据我测试,该代码在桌面浏览器
我使用appendTo将一组选定的元素添加到某个DOM元素: $('#incoming-items').appendTo($('#item-list')); (#incoming products 是
我正在使用 appendTo 将子 div 从一个父级移动到另一个父级。这是功能的一部分: $('#forum_holder_hidden').children().slice(0,5).append
这个问题在这里已经有了答案: Event binding on dynamically created elements? (23 个回答) 关闭 7 年前。
我基本上遇到了这个错误 Uncaught SyntaxError: Unexpected token } 这是哪一行 s.append( ); } '); var items = []
我使用 jQuery 在表格中添加行,带有一些输入/选择。我有一个显示的模板行:无,为了添加一个新行,我复制了这一行并将它放在我的表的 tbody 末尾。一些代码: CSS: .template {
我正在尝试将一些表单数据提交到 Servlet使用JQuery并检索 Servlet来自同一个的回应 JQuery 。请看下面的代码。 JSP Pa
我正在尝试将 ckeditor appendTo 方法与 javascript 中指定的配置选项结合使用: var config = {tabSpaces: 4}; editor1 = CKEDITO
因此,我向页面添加了四个独立于 Wordpress 循环的元素。我使用 jQuery appendto() 将它们添加到帖子列表中(循环)。除了最后一个之外,它工作正常......它似乎无法使用最后一
我是一名优秀的程序员,十分优秀!