IE8 Jquery Javascript “Error: Object required” Bug

五迷三道 提交于 2019-12-13 03:55:45

问题


IE8 throws an "Error: Object required" message (error in the actual jquery library script, not my javascript file) when the switch statement in this function runs. This code works in IE6, IE7, FF3, and Safari... Any ideas? Does it have something to do with the '$(this)' selector in the switch? Thanks!

function totshirts(){
    $('.shirt-totals input').val('0');
    var cxs = 0;
    var cs = 0;
    var cm = 0;
    $.each($('select.size'), function() {
        switch($(this).val()){
            case "cxs":
                cxs ++;
                $('input[name="cxs"]').val(cxs);
                break;
            case "cs":
                cs ++;
                $('input[name="cs"]').val(cs);
                break;
            case "cm":
                cm ++;
                $('input[name="cm"]').val(cm);
                break;
        }
    });
}

回答1:


Oh noes, don't do it that way at all.

Do something more along the lines of this:

$('.shirt-totals input').val('0');
$('select.size').each(function() {
 var name = $(this).attr('name');
 var currVal = parseInt($("input[name='"+name+"']").val());
 $("input[name='"+name+"']").val(currVal+1);
});

As a sidenote, I tend to find that jQuery seems to deal with single quotes better than doubles in when doing the "equals" comparison.




回答2:


I upgraded the jQuery library from 1.2.6 to 1.3.2 and this solved the problem. Didn't realize I had an old version- oops.

Thanks for your help all!



来源:https://stackoverflow.com/questions/787074/ie8-jquery-javascript-error-object-required-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!