我在静态页面上有一系列HTML复选框。我需要添加功能;如果选中了复选框,则立即内联将父div(或包装div)背景容器颜色更改为绿色
我有现有的功能和行为,其中复选框生成为pdf,未检查的pdf隐藏。我也在选择时在这些包装器div中交换html内容。现在我只想用这些选择切换背景颜色样式。因此,在不破坏之前的情况下添加此功能。
JS:
$(document).ready(function() {
texts = {
item1: 'Item Box 1 Content <strong>html</strong> right here! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
item2: 'Now its Item Box 2 <strong>html</strong> content here ! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
item3: 'This is the example <strong>html</strong> of Item box 4! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
item4: 'Item box number 5 <strong>html</strong> content is here! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
}
$("#container").css('background', '#fff')
$('.download-pdf').click(function() {
notChecked = $("input[type=checkbox]:not(:checked)").parent();
notChecked.hide();
yesChecked = $("input[type=checkbox]:checked").parent();
$.each(yesChecked, function( index, el ) {
$(el).show().html(texts[$(el).attr('id')]);
});
var pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML(document.getElementById('records'), function() {
// add here
setTimeout(function(){
location.reload();
},3000);
});
var file = 'test';
if (typeof doc !== 'undefined') {
doc.save(file + '.pdf');
} else if (typeof pdf !== 'undefined') {
setTimeout(function() {
pdf.save(file + '.pdf');
// $("#item4").hide();
}, 2000);
} else {
alert('Error 0xE001BADF');
}
});
});
div标记如下:
<div class="row" id="item1" class="box">
Item 1 Details for the PDF test.<br>
<input type="checkbox" id="check1" name="sample[]" value="red"/> <em>This</em> is a checkbox1
<br />
</div>
答案 0 :(得分:2)
http://codepen.io/anon/pen/YwXpLy
$('#check1').on('click', function() {
if ($(this).is(':checked'))
$(this).parent('div').css('background-color', 'green');
else
$(this).parent('div').css('background-color', '');
});