2012-10-12 59 views
0

我正在使用jQuery转换一堆选择元素到收音机按钮。
然后,我想要更改CHECKED特定RADIO按钮的状态,当用户单击某个特定的时,选择选项。为此,我做了一些检查并设置了一些变量。
在Chrome和Firefox中一切正常,但不是其他浏览器。 Opera的控制台告诉我,只要向用于检查哪些RADIO按钮已被用户检查过的变量添加“:checked”,该变量就是未定义的(所以函数的其余部分不再工作)。这里是我用于该变量的代码:检查选择器不工作在Opera和IE动态生成单选按钮

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元素单选按钮(从#1得到这个)代码:

$('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

我的问题是偏题:我不明白你的代码'$(“”,window.parent.document)'。你能给我一个解释吗?谢谢。 – reporter

+0

jQuery被放置在一个iFrame中的HTML文件中,我从中操纵它的父项。 – Marc

+0

我查找jquery的文档,并在那里找到你的定义。显然,从1.4.x开始,可以使用这种方式。因为你没有发布你的源代码,我不得不创建自己的 - 一个非常简单的例子。在所有浏览器中,我的示例都工作 - 即使在IE6和IE7中 - 除Chrome之外。他拒绝访问父母元素(这里是'window.parent.document')。 – reporter

回答

0

我看到表示console.log的IE 8错误没有定义。从代码中移除console.log解决了IE中的问题,现在它可以在每个浏览器中正确运行。

相关问题