【发布时间】:2017-02-03 14:32:36
【问题描述】:
我有一个简单的切换复选框功能,可以遍历产品包以选择它们。我以输入复选框的方式创建了它,以便能够捕获值。我不明白的是如何捕获仅显示复选框时的值。
我知道要捕获我会这样做的价值:
$('#package2').val();
但是如何仅在“激活”/“选择”时获取该值。然后,一旦它被选中,我就有了我想要在“产品选择”旁边显示的值。
此外,您可以在 sn-p 或 fiddle 中看到,当您单击两个框,然后再次单击一个框以取消选择它时,“继续”消失了。是否还有一种方法可以在选中任何框时保持显示?
$('.calendar-check').on('change', function() {
if ($(this).prop('checked')) {
$(this).parents('.product-wrap:first').find('.checkmark-img').show('200');
$('#next1').show();
} else {
$(this).parents('.product-wrap:first').find('.checkmark-img').hide('200');
$('#next1').hide();
}
});
.package-img {
width: 60%;
height: auto;
margin-left: 20%;
cursor: pointer;
transition:1s; -webkit-transition:1s;
position: relative;
}
#calendar-wrap, #tp-wrap {
width: 100%;
position: relative;
}
.checkmark-img {
display: none;
width: 40%;
height: auto;
z-index: 1;
cursor: pointer;
}
.proceed-btn {
display: none;
transition:.5s; -webkit-transition:.5s;
}
.calendar-check {
display: none;
}
.package-check-toggle {
position: relative;
height: 100%;
width: 100%;
display: block;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="left-container">
<div id="calendar-wrap" class="product-wrap">
<h2 class="product-titles">Package 1</h2>
<label for="package1" class="package-check-toggle">
<img src="images/calendar-package.png" alt="Package 1" class="package-img" id="calendar-img">
<img src="images/checkmark-circle.png" class="checkmark-img total-center">
</label>
<input type="checkbox" class="calendar-check" id="package1" value="Photo Gift">
</div>
</div>
<div class="right-container">
<div id="tp-wrap" class="product-wrap">
<h2 class="product-titles">Package 2</h2>
<label for="package2" class="package-check-toggle">
<img src="images/tp-package.png" alt="Package 2" class="package-img" id="tp-img">
<img src="images/checkmark-circle.png" class="checkmark-img total-center">
</label>
<input type="checkbox" class="calendar-check" id="package2" value="Touch Points">
</div>
</div>
Product chosen
<div class="proceed-btn" id="next1">PROCEED</div>
【问题讨论】:
-
$(this).val()
标签: javascript jquery css function onchange