我在尋找幫助爲什麼此代碼不適用於列的所有標題。此代碼適用於任何列的第一時間,但不會第二次觸發點擊事件。爲什麼$(ColTh).click(function()
未被連接到所有th
的標頭中?無法綁定所有列標題的點擊事件。僅工作一次
$("table.ui-datepicker-calendar > thead").find("th[scope='col']").each(function() {
var ColTh = $(this);
$(ColTh).click(function() {
var colIndex = $(ColTh).index();
var dateList = [];
$(ColTh).closest('table').find('tr').each(function() {
var dateTd = $(this).find('td:eq(' + colIndex + ')');
var month = $(dateTd).attr('data-month'); //.trigger('click');
var year = $(dateTd).attr('data-year');
var day = $(dateTd).children(0).text();
if (typeof month != 'undefined') {
var monthINT = parseInt(month.trim());
monthINT = monthINT + 1;
var newDate = monthINT + '-' + day.trim() + '-' + year.trim();
dateList.push(newDate);
}
});
$('#itemSchedulerCal').multiDatesPicker('addDates', dateList);
dateList = [];
});
});
我想這是因爲日期選擇器功能再次重繪的所有元素,因此,因爲是新的元素並沒有綁定的事件了。你需要使用委託的功能,並沒有必要循環與每個... – DaniP
爲什麼不只是使用委派? (''click','th [scope ='col']「,function(){var ColTh = this,colIndex = $(ColTh).index (); ....})' –
我怎麼可以在這裏使用委託? – youngseagul