简体   繁体   English

Firefox不会在第二次点击时更新价值

[英]Firefox won't update value on second click

So I've taken over a project that is almost ready to launch, a site where you can lease cars. 因此,我已经接管了一个几乎可以启动的项目,可以在这里租车。 I've uploaded the main part of the service here: http://erikblomqvist.se/junk/car/ 我已经在这里上传了服务的主要部分: http : //erikblomqvist.se/junk/car/

Everything works smoothly except for the color changing function (the colored boxes under the headline Färgalternativ ). 除了换色功能(标题为Färgalternativ的彩色框)以外,其他所有功能均正常运行。 It's supposed to update the price on the colors from brown to light gray (#4 - #8) – those are a bit more expensive, since they are in metallic. 它应该将颜色的价格从棕色更新为浅灰色(#4-#8),因为它们是金属色的,所以价格要贵一些。 In Chrome, this works as planned, but in Firefox, if I first select a metallic color, then a non-metallic, and THEN a the same metallic again, the price won't change back. 在Chrome中,这可以按计划工作,但是在Firefox中,如果我先选择金属色,然后选择非金属色,然后再选择相同的金属色,则价格不会改变。 It changes correctly the first time, but not the second time I click that metallic color. 第一次正确更改,但是第二次单击该金属色时更改不正确。 In Safari, the price doesn't change at all (I'm guessing that if the Firefox problem gets solved, Safari gets solved as well). 在Safari中,价格根本没有变化(我猜想,如果Firefox问题得到解决,Safari也将得到解决)。

The function is based on a data-name on the color boxes, that gets checked with this function: 该功能基于颜色框上的data-name ,该data-name通过此功能进行检查:

$( '#car-colors .color' ).each(function() {
    $( this ).on( 'click', function() {
        selected_color = undefined; 

        var color_name = $( this ).data('name');

        $( '#car-colors .color' ).not( this ).removeClass('selected');
        $( this ).addClass('selected');

        $( 'option', color_select ).each( function() {
            if( $( this ).val() == color_name ) {
                color_select.find( 'option' ).removeAttr('selected');
                $( this ).attr('selected', 'selected');
            }
        });

        $( '.selected-color-name' ).fadeIn();
        $( '.selected-color-name span' ).html( color_name );

        var selected_color = color_select.children(':selected');

        checkSelectedColor(selected_color);
    });
});

The variable color_select is defined as $( '#order-color-select' ) . 变量color_select定义为$( '#order-color-select' )

The function checkSelectedColor is defined here: 函数checkSelectedColor在此处定义:

function checkSelectedColor(selected_color) {
    if( selected_color.data('is-metallic') == 'yes' ) {
        color_checkbox.prop('checked', true);
    } else {
        color_checkbox.prop('checked', false);
    }

    color_input.val( selected_color.val() );                
    calculatePrice();
}

I've added stuff like selected_color = undefined to make sure that the variable is reseted, but after a color that has an <option data-is-metallic="yes"> (inside of #order-color-select ) is selected a second time, it handles the value as "no" instead of "yes". 我添加了诸如selected_color = undefined ,以确保重置了该变量,但是在选择了具有<option data-is-metallic="yes"> (在#order-color-select#order-color-select第二次,它将值处理为“否”而不是“是”。

I can't get my head around on why this is, especially only in Firefox/Safari. 我无法理解为什么会这样,尤其是在Firefox / Safari中。

I've included the beautified version of the car functions here: http://pastebin.com/i5bup5rx 我在此处包含了汽车功能的美化版本: http : //pastebin.com/i5bup5rx

Appreciate any kind of help that could lead me in the right direction of getting this solved! 感谢任何可以帮助我正确解决问题的帮助!

Thanks 谢谢

I think the error is in setting the selected option in the select element using .attr() . 我认为错误在于使用.attr()在select元素中设置选定的选项。

Instead you can just set the value of the select element like 相反,您可以只设置select元素的值,例如

$('#car-colors .color').on('click', function() {
  var color_name = $(this).data('name');

  $('#car-colors .color').not(this).removeClass('selected');
  $(this).addClass('selected');

  $('#order-color-select').val(color_name);

  $('.selected-color-name').fadeIn();
  $('.selected-color-name span').html(color_name);

  var selected_color = color_select.children(':selected');

  checkSelectedColor(selected_color);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM