[英]jQuery - dynamically created select boxes not triggering function when value is selected
我有一些動態創建選擇下拉菜單的代碼。 我希望每個選擇下拉菜單都有一個“onchange”能力,一旦選擇就調用一個不同的功能...到目前為止,我很難做到這一點...幫助表示贊賞..謝謝!
function createSections(num) {
var value = num.value;
alert(value);
}
jQuery('#number-of-units').change(function() {
var num = parseInt(jQuery(this).val());
var container = jQuery('<div />');
var i = 1;
for(i; i <= num; i++) {
container.append('<select onchange="createSections(this)" class="create-course-select-element"><option disabled selected value="none">--</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select>');
}
jQuery('#display-unit').html(container).hide();
jQuery('#display-unit').fadeIn(1500);
});
您可以使用jQuery函數“on”。
這將激活所有<select>
進入<body>
jQuery('body').on('change', 'select', function() { alert('fire the event!'); });
關閉之前我的2美分:
將select元素附加到文檔后, 也可以調用change()函數。
MapSelects() //called AFTER adding the select to the document
function MapSelects(){
$('select.create-course-select-element').off();
$('select.create-course-select-element').change(function(){
createSections($(this).val());
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.