选中Opera和IE中不工作的选择器以获取动态生成的单选按钮



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

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问题吗?

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

最新更新