我正在使用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問題?
我的問題是偏題:我不明白你的代碼'$(「」,window.parent.document)'。你能給我一個解釋嗎?謝謝。 – reporter
jQuery被放置在一個iFrame中的HTML文件中,我從中操縱它的父項。 – Marc
我查找jquery的文檔,並在那裏找到你的定義。顯然,從1.4.x開始,可以使用這種方式。因爲你沒有發佈你的源代碼,我不得不創建自己的 - 一個非常簡單的例子。在所有瀏覽器中,我的示例都工作 - 即使在IE6和IE7中 - 除Chrome之外。他拒絕訪問父母元素(這裏是'window.parent.document')。 – reporter