【问题标题】:jQuery appendTo(), json not working in IE 6,7,8jQuery appendTo(), json 在 IE 6,7,8 中不起作用
【发布时间】:2009-09-30 16:27:28
【问题描述】:

这两天我一直在绞尽脑汁想找到解决办法。我正在使用 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() 不起作用。

有人有什么想法吗?

【问题讨论】:

    标签: javascript php jquery json


    【解决方案1】:

    我有点惊讶 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);
    }
    

    【讨论】:

    • 你愿意给我看一个例子吗?我边走边学?
    • sculiffe,你太棒了。谢谢!
    • 呵呵,如果它有效,我很高兴......我只是希望这个过程不要那么复杂......对于每个浏览器。
    • 哇哦谢谢!!!我一直在寻找这个问题的答案。如果我能对这个答案投票一百次,我会的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多