仅限IE7中的jQuery问题

时间:2012-10-22 21:04:06

标签: javascript jquery internet-explorer-7

我在IE7中突然出现问题,只有当"Object doesn't support the property or method"事件发生onChange个事件时才会出现VariationSelect错误。因此我把它缩小到以下几点:

$(".VariationSelect").change(function() {
    // a bunch of irrelevant code
    // get the index of this select
    var index = $('.VariationSelect').index($(this)); //this is the line throwing the error in IE7
           //some code that returns a block of data in json formatting

});

我的第一个想法是使用attr('disabled', 'disabled')的代码段,因为我之前在使用removeAttr时在IE7中遇到了麻烦,但即使我删除了这些行,错误也保持不变和行JS错误中的/ char引用(当然没有意义)不会改变。所以你在IE7不接受的代码块中看到了其他什么吗?

编辑:更改选择框后代码正在运行。选择框HTML如下所示:

<div class="DetailRow">
<div class="Label">Some Label:</div>
<div class="Value">
    <select name="variation[aNumberStartingAtOneAndIncrementingUpwards]" class="VariationSelect" id="VariationSelect" style="width: 180px;">
        <option value="">-- Choose an option --</option>
        {A bunch of options for this choice loaded by PHP}
    </select>
</div>
</div>

快速而肮脏的方法,因为我知道name元素总是有一个比索引大的数字,就是从改变的元素中获取名称,如:

    var index = $(this).attr('name');
index = index.replace('variation[','');
index = index.replace(']','');
index = (index*1)-1;

是否有更快/更好的方法来获取索引?

1 个答案:

答案 0 :(得分:1)

在jQuery中使用该attr函数时,它必须是你在线上运行的唯一函数。

要修复,我更改了代码:

    $('.VariationSelect:eq(' + (index + 1) + ')').append(data.options).attr('disabled', '').focus(); 

为:

    $('.VariationSelect:eq(' + (index + 1) + ')').append(data.options); $('.VariationSelect:eq(' + (index + 1) + ')').attr('disabled', ''); 
    $('.VariationSelect:eq(' + (index + 1) + ')').focus();

它现在可以在IE7中运行,并且可以继续在其他所有浏览器中运行。我猜我可能会将追加和焦点功能结合起来,但是,它正在发挥作用。