IE中选择列表的jQuery代码无法正常工作

时间:2012-08-06 13:25:44

标签: javascript jquery internet-explorer

我在IE中遇到select列表有问题(所有版本)。我有两个方向依赖于City1和City2字段的字段。当我从City1选择城市时,方向字段应该只显示4个方向。同样,在City2字段中。在Firefox,Chrom,Opera中一切正常,但在IE中没有 - 我看到了整个方向列表。

在我看来 - 这个代码有问题,但我不知道:

    if (parseInt($("#finish_day").val()) < 4) {
        $('#return_car').children('#return_car option[value=' + $('#get_car').val() + ']').attr('selected', 'selected').siblings().attr('disabled', true);
        if ($('#return_car').val()) $('#return_car').change();
    } else {
        $('#get_car > option, #return_car > option').prop('disabled', false);
        });
    }

工作代码在这里:http://jsfiddle.net/utXMD/

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:1)

删除$ .viewMap_get和$ .viewMap_return对象中最后一个值后的逗号:

$.viewMap_get = {
        '0' : $([]),
        'Katowice' : $('#c1a, #c1b, #c6, #c7'),
        'Kraków' : $('#c2a, #c2b, #c6, #c7'),
        'Warszawa' : $('#c3a, #c3b, #c6, #c7'),
        'Wrocław' : $('#c4a, #c4b, #c6, #c7'),
        'Gdańsk' : $('#c5a, #c5b, #c6, #c7')
    };



$.viewMap_return = {
        '0' : $([]),
        'Katowice' : $('#d1a, #d1b, #d7'),
        'Kraków' : $('#d2a, #d2b, #d7'),
        'Warszawa' : $('#d3a, #d3b, #d7'),
        'Wrocław' : $('#d4a, #d4b,, #d7'),
        'Gdańsk' : $('#d5a, #d5b, #d7')
    };

IE7和IE8存在JS对象值的问题,这些值以尾随','结尾。

更新:

请尝试以下代码:

$(function() {

    $( "#date_from, #date_to" ).datepicker({
        defaultDate: "+1d",
        minDate: "+1d",
        changeMonth: true,
        changeYear: true,
        dateFormat: "dd/mm/yy",
        monthNamesShort: ["Sty","Lut","Mar","Kwi","Maj","Cze","Lip","Sie","Wrz","Paz","Lis","Gru"],
        dayNamesMin: ["Nie", "Pon", "Wt", "Sr", "Czw", "Pt", "Sob"],
        numberOfMonths: 1,
        onSelect: function( selectedDate ) {
            if(this.id == 'date_from'){
              var dateMin = $('#date_from').datepicker("getDate");
              var dateMax = $('#date_to').datepicker('getDate');
              var rent_date_from = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); 
              var rent_date_to = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 29); 
              $('#date_to').datepicker("option","minDate",rent_date_from);
              $('#date_to').datepicker("option","maxDate",rent_date_to);      
            }
            compute(); //Add compute
        }

    }); 

    $.viewMap_get = {
        '0' : $([]),
        'Katowice' : $('#c1a, #c1b, #c6, #c7'),
        'Kraków' : $('#c2a, #c2b, #c6, #c7'),
        'Warszawa' : $('#c3a, #c3b, #c6, #c7'),
        'Wroclaw' : $('#c4a, #c4b, #c6, #c7'),
        'Gdansk' : $('#c5a, #c5b, #c6, #c7')
    };

    $.each($.viewMap_get, function() { $(this).remove(); });
    $('#get_city_district').hide();

    $('#get_car').on('change', function() {
        // hide all
        $.each($.viewMap_get, function() { $(this).remove(); });
        $('#get_city_district').hide();
        // show current
        $('#get_city_district').append($.viewMap_get[$(this).val()]).show();

        var id = $.viewMap_get[$(this).val()].attr("id");   
        $('#'+id).attr('selected', 'selected');
    });

    $.viewMap_return = {
        '0' : $([]),
        'Katowice' : $('#d1a, #d1b, #d7'),
        'Kraków' : $('#d2a, #d2b, #d7'),
        'Warszawa' : $('#d3a, #d3b, #d7'),
        'Wroclaw' : $('#d4a, #d4b,, #d7'),
        'Gdansk' : $('#d5a, #d5b, #d7')
    };

    $.each($.viewMap_return, function() { $(this).remove(); });
    $('#return_city_district').hide();

    $('#return_car').on('change', function() {

        $.each($.viewMap_return, function() { $(this).remove(); });
        $('#return_city_district').hide();
        // show current
        $.viewMap_return[$(this).val()].show();
        $('#return_city_district').append($.viewMap_return[$(this).val()]).show();

        var id = $.viewMap_return[$(this).val()].attr("id");
        $('#'+id).attr('selected', 'selected');
    });

    function compute() {
        var c = $('select#time_from_hour').val();
        var d = $('select#time_from_minute').val();
        var e = $('select#time_to_hour').val();
        var f = $('select#time_to_minute').val();
        var g = $('input#date_from').val();
        var h = $('input#date_to').val();
        date1= g.split("/");
        var timestamp = (new Date(date1[2],date1[1],date1[0],c,d).getTime()/1000) + 7200;
        $('#total_hour1').val(timestamp);

        date2= h.split("/");
        var timestamp = (new Date(date2[2],date2[1],date2[0],e,f).getTime()/1000) + 7200;
        $('#total_hour2').val(timestamp);

        var x = -$('input#total_hour1').val();
        var y = -$('input#total_hour2').val();
        var total_time = x - y;
        result = total_time / 86400;
        new_number = Math.ceil(result);

        if(isNaN(new_number)) {
          var new_number = 0;
        }
        $('#finish_day').val(new_number);

        if( parseInt($("#finish_day").val()) < 4 ) {   
            $('#return_car').children('#return_car option[value=' + $('#get_car').val() + ']').attr('selected', true).siblings().attr('disabled', true);

            if ($('#return_car').val()) $('#return_car').change();
        }
        else {
            $('#get_car > option, #return_car > option').prop('disabled', false);
            $('#three_day').hide(2000, function () {
                $(this).remove();
            });
        }

    }


    $('select#time_from_hour').change(compute);
    $('select#time_from_minute').change(compute);
    $('select#time_to_hour').change(compute);
    $('select#time_to_minute').change(compute);
    // $('select#return_car').change(compute);
    $('select#get_car').change(compute);
    $('input#finish_day').change(compute); 

});

$(function() { $( "#date_from, #date_to" ).datepicker({ defaultDate: "+1d", minDate: "+1d", changeMonth: true, changeYear: true, dateFormat: "dd/mm/yy", monthNamesShort: ["Sty","Lut","Mar","Kwi","Maj","Cze","Lip","Sie","Wrz","Paz","Lis","Gru"], dayNamesMin: ["Nie", "Pon", "Wt", "Sr", "Czw", "Pt", "Sob"], numberOfMonths: 1, onSelect: function( selectedDate ) { if(this.id == 'date_from'){ var dateMin = $('#date_from').datepicker("getDate"); var dateMax = $('#date_to').datepicker('getDate'); var rent_date_from = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); var rent_date_to = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 29); $('#date_to').datepicker("option","minDate",rent_date_from); $('#date_to').datepicker("option","maxDate",rent_date_to); } compute(); //Add compute } }); $.viewMap_get = { '0' : $([]), 'Katowice' : $('#c1a, #c1b, #c6, #c7'), 'Kraków' : $('#c2a, #c2b, #c6, #c7'), 'Warszawa' : $('#c3a, #c3b, #c6, #c7'), 'Wroclaw' : $('#c4a, #c4b, #c6, #c7'), 'Gdansk' : $('#c5a, #c5b, #c6, #c7') }; $.each($.viewMap_get, function() { $(this).remove(); }); $('#get_city_district').hide(); $('#get_car').on('change', function() { // hide all $.each($.viewMap_get, function() { $(this).remove(); }); $('#get_city_district').hide(); // show current $('#get_city_district').append($.viewMap_get[$(this).val()]).show(); var id = $.viewMap_get[$(this).val()].attr("id"); $('#'+id).attr('selected', 'selected'); }); $.viewMap_return = { '0' : $([]), 'Katowice' : $('#d1a, #d1b, #d7'), 'Kraków' : $('#d2a, #d2b, #d7'), 'Warszawa' : $('#d3a, #d3b, #d7'), 'Wroclaw' : $('#d4a, #d4b,, #d7'), 'Gdansk' : $('#d5a, #d5b, #d7') }; $.each($.viewMap_return, function() { $(this).remove(); }); $('#return_city_district').hide(); $('#return_car').on('change', function() { $.each($.viewMap_return, function() { $(this).remove(); }); $('#return_city_district').hide(); // show current $.viewMap_return[$(this).val()].show(); $('#return_city_district').append($.viewMap_return[$(this).val()]).show(); var id = $.viewMap_return[$(this).val()].attr("id"); $('#'+id).attr('selected', 'selected'); }); function compute() { var c = $('select#time_from_hour').val(); var d = $('select#time_from_minute').val(); var e = $('select#time_to_hour').val(); var f = $('select#time_to_minute').val(); var g = $('input#date_from').val(); var h = $('input#date_to').val(); date1= g.split("/"); var timestamp = (new Date(date1[2],date1[1],date1[0],c,d).getTime()/1000) + 7200; $('#total_hour1').val(timestamp); date2= h.split("/"); var timestamp = (new Date(date2[2],date2[1],date2[0],e,f).getTime()/1000) + 7200; $('#total_hour2').val(timestamp); var x = -$('input#total_hour1').val(); var y = -$('input#total_hour2').val(); var total_time = x - y; result = total_time / 86400; new_number = Math.ceil(result); if(isNaN(new_number)) { var new_number = 0; } $('#finish_day').val(new_number); if( parseInt($("#finish_day").val()) < 4 ) { $('#return_car').children('#return_car option[value=' + $('#get_car').val() + ']').attr('selected', true).siblings().attr('disabled', true); if ($('#return_car').val()) $('#return_car').change(); } else { $('#get_car > option, #return_car > option').prop('disabled', false); $('#three_day').hide(2000, function () { $(this).remove(); }); } } $('select#time_from_hour').change(compute); $('select#time_from_minute').change(compute); $('select#time_to_hour').change(compute); $('select#time_to_minute').change(compute); // $('select#return_car').change(compute); $('select#get_car').change(compute); $('input#finish_day').change(compute); });

答案 1 :(得分:0)

IE中的选项元素仅支持一些css属性,如colorbackground-color,IE9中不支持font- *属性,display,text- *(可能在IE10中?)。当你使用display:none隐藏选项时,没有任何反应。

解决方案是仅添加当前城市的路线。例如:

$(function() {
    var directions =  {
          "city1": ["direction1", "direction2" ],
          "city2": ["direction3", "direction4" ]
       };

    $("#scity").change(function() {  
        var cityDirections = directions[$(this).val()];
        var $directions = $("#citydirections");
        $directions.children().remove();

        for(var i = 0; i < cityDirections.length; i++)
           $directions.append("<option value='" + cityDirections[i] + "'>" + cityDirections[i] + "</option>");
    });
});

http://jsfiddle.net/8t7J6/4/