【问题标题】:Selecting option in select field does not trigger ajax script in Google Chrome在选择字段中选择选项不会触发 Google Chrome 中的 ajax 脚本
【发布时间】:2011-09-13 16:06:54
【问题描述】:

在选择字段中的选项时,无法在 Google Chrome 和 Internet Explorer 中触发 ajax 响应。不过,它确实适用于所有其他浏览器。

这是html:

<select>
  <option class="showDiv" data-div="0,1,0,mw1,default" data-vars="2136,1|10|1|0" selected="">added</option>
  <option class="showDiv" data-div="0,1,0,mw1,default" data-vars="2136,1|10|1|1">title</option>
  </select>

这里是 ajax 部分:

$('.showDiv').live("click", function () {
    var divs = $(this).attr('data-div');
    var vars = $(this).attr('data-vars');
    divs = divs.split(",");
    $.ajax({
        type: "post",
        url: "crt/run_script.php",
        data: {
            divs: divs,
            vars: vars,
        },
        beforeSend: function () {
            centerWin("loading");
        },
        complete: function () {
            $("#loading").hide("fast");
        },
        success: function (html) {
            $("#mainWin").html('');
            $("#mainWin").html(html);
        }
    });
});

【问题讨论】:

  • 您使用的是哪个版本的 Chrome、IE 和 jQuery?
  • Chrome 最新版本。 IE 8(不是因为我想要,而是因为它必须在 IE 8 上工作:P)

标签: jquery ajax google-chrome internet-explorer-8


【解决方案1】:

您为什么不这样做而不是更改选择元素?

$('#selectID').change(function(){
   ...
});

要获得选中的选项,您可以这样做:

$('#selectID').change(function(){
   var index = this.selectedIndex;
   var option = $(this.options[index]);
   //rest of the code use `option` instead of `this` in your code
   ...
});

【讨论】:

  • 您能说得更具体些吗?我不知道如何重写代码以便它使用您的建议
  • @mick_smick 我添加了怎么做。
  • 太棒了。你摇滚。我已将脚本更改为: $('.showSelect').live( "change",function(){ var index = this.selectedIndex; var option = $(this.options[index]); var divs = $ (option).attr('data-div'); ...它成功了。再次感谢。
  • 不幸的是,它仍然不能在 IE 中工作 :(。但至少它在 Chrome 中工作 :)。
  • @mick_smick 获取如下数据属性:$(option).data('div')...等。不要用attr(...)得到它
猜你喜欢
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 2012-03-12
  • 2018-03-05
  • 2015-11-03
  • 1970-01-01
相关资源
最近更新 更多