2015-12-09 46 views
0

我在静态页面上有一系列HTML复选框。我需要添加功能;如果选择了一个复选框,立即在线父DIV(或包装DIV)背景容器颜色更改为绿色当检查/选择框被选中时更改父div的背景颜色

我有现有的功能和行为,其中检查框生成到PDF和非托运PDF文件隐藏。我也选择这些包装divs内的交换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 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

我看不出有任何的div在这里(纠正我,如果我错了)。你可以请张贴那些div的HTML吗? – Franco

+0

嗨James Franco,在css'.green {background-color:green;}'中更新了 –

+0

,然后获得父母并执行'toggleClass(“green”)' –

回答

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', ''); 
}); 
相关问题