选中的选择器在Opera和IE中不能用于动态生成的单选按钮

时间:2012-10-12 10:48:57

标签: javascript jquery

我正在使用jQuery将一堆 SELECT 元素转换为 RADIO 按钮。
然后我想更改 CHECKED 当用户点击特定的 SELECT 选项时,特定 RADIO 按钮的状态。为此,我做了一些检查并设置了一些变量。
一切都在Chrome和Firefox中按预期工作,但不是其他浏览器。 Opera的控制台告诉我,只要我将“:checked”添加到我用来检查用户检查了哪些RADIO按钮的变量中,该变量是未定义的(因此该函数的其余部分不再起作用)。这是我用于该变量的代码:

var bedrukking = $("form#specificatie input:checked[name='staffeldim_5287\\:1192']", window.parent.document).val();

关于为什么失败的任何想法?我不能直接操纵DOM,jQuery是我唯一的选择。

以下是更改选中选项的全部代码:

// Verander Bedrukking als Papier wordt ingesteld op 90 of 80 grams wit bankpost
$("form#specificatie select#staffeldim_5285\\:1192", window.parent.document).on("change", function(){
    var papier = $(this).val();
    // 90 grams wit bankpost FSC lasergeschilkt is value 27567, 80 grams wit bankpost FSC lasergeschilkt is value 27565
    if (papier == "27567" || papier == "27565") {
        var bedrukking = $("form#specificatie input:checked[name='staffeldim_5287\\:1192']", window.parent.document).val();
        console.log(bedrukking);
        // 1 zijdig full-color is value 27579, 2 zijdig full-color is 27580
        if (bedrukking == "27579" || bedrukking == "27580") {
            // Zet de value van Bedrukking op iets anders en laat hem knipperen om aandacht te trekken
            $("form#specificatie input[name='staffeldim_5287\\:1192']:checked", window.parent.document).removeAttr('checked');
            $("form#specificatie input[name='staffeldim_5287\\:1192'][value='27583']", window.parent.document).attr('checked', 'checked').animate({opacity: 0}, 200, "linear", function() {
                $(this).animate({opacity: 1}, 200);
            });
            // Geef aan dat deze combinatie niet mogelijk is
            $('table.staffels #combinatieControle', window.parent.document).remove(); // Verwijder de waarschuwing indien hij al aanwezig is, voordat we een nieuwe toevoegen
            $("table.staffels", window.parent.document).eq(0).prepend('<tr id="combinatieControle"><td><div style="margin-bottom: 15px; color: red;">Full-color bedrukking kan niet worden gecombineerd met 80 en 90 grams wit papier. (Deze combinatie is wel mogelijk met het artikel "budget briefpapier".) Uw specificaties zijn gewijzigd.</div></td></tr>');
        }
    }
    // Deze combinatie is wel mogelijk, dus verwijder de waarschuwing indien hij aanwezig is
    else {
        $('table.staffels #combinatieControle', window.parent.document).remove();
    }
});

那里有一些荷兰评论,希望你不介意。这是我用来将SELECT元素更改为RADIO按钮的代码(从Stackoverflow获取):

$('form#specificatie select', window.parent.document).each(function(i, select){
    var $select = $(select);

    // Als er meer dan 10 opties zijn voor de huidige select, converteren we hem niet naar radio buttons
    var nrOptions = $(this).find('option').length;
    if (nrOptions > 10) {
        return
    }

    var counter = 1
    $select.find('option').each(function(j, option){
        var $option = $(option);
        // Create a radio:
        var $radio = $('<input type="radio" />');
        // Set name and value:
        $radio.attr('name', $select.attr('name')).attr('value', $option.val());
        // Set ID:
        $radio.attr('id', $option.val() + '_' + counter );
        // Set checked if the option was selected
        if ($option.attr('selected')) $radio.attr('checked', 'checked');
        // Insert radio before select box:
        $select.before($radio);
        // Insert a label:
        $select.before(
          $("<label />").attr('for', $option.val() + '_' + counter).text($option.text())
        );
        // Insert a <br />:
        $select.before("<br/>");

        counter++;
    });
    $select.remove();
});

更新:我尝试将jQuery版本从1.8.1更改回1.7.1:现在一切都适用于除Internet Explorer 8和9之外的所有浏览器。为什么jQuery版本会有所不同?我可以尝试解决IE问题吗?

1 个答案:

答案 0 :(得分:0)

我看到IE 8错误,表示console.log未定义。从代码中删除console.log修复了IE中的问题,现在它在每个浏览器中都能正确运行。