我冒昧地重構了大部分代碼。希望它可以幫助
// These elements don't change so no point getting them on each click
var modal = $('#transparent').on('click.close', function() {
$(this).hide(); // hide the element on click
}),
username_input = modal.find('input[name=username]'),
email_input = modal.find('input[name=email]'),
day_checkboxes = modal.find('input[type=checkbox]');
$('#close').on('click', function() {
modal.trigger('click.close'); // trigger the "close" click event
});
$('#splash').on('click', function(e) {
e.stopPropagation(); // don't allow clicks to leave the #splash element
});
$('.data').on('click', function(e) {
// Get all the data from within this element (row)
var row = $(this),
username = row.find('.row_user').text(),
email = row.find('.row_email').text(),
days = row.find('.row_days').text().toLowerCase();
// split the days by comma, defaulting to an empty array
days = days.length && days.split(',') || [];
modal.show();
username_input.val(username);
email_input.val(email);
day_checkboxes.prop('checked', function() {
// looks for the "value" in the days array and sets the "checked" property
return ~days.indexOf(this.value);
});
});
演示 - http://jsfiddle.net/Y4wGG/7/
不錯的重構,但是,我不確定OP是否能夠理解和欣賞它。 –
@SamuelLiew添加評論 – Phil
我當然感謝你的努力,絕對不明白:D lol –