【问题标题】:Getting value from checkbox onchange event从复选框 onchange 事件中获取价值
【发布时间】:2017-02-03 14:32:36
【问题描述】:

我有一个简单的切换复选框功能,可以遍历产品包以选择它们。我以输入复选框的方式创建了它,以便能够捕获值。我不明白的是如何捕获仅显示复选框时的值。

我知道要捕获我会这样做的价值:

$('#package2').val();

但是如何仅在“激活”/“选择”时获取该值。然后,一旦它被选中,我就有了我想要在“产品选择”旁边显示的值。

此外,您可以在 sn-p 或 fiddle 中看到,当您单击两个框,然后再次单击一个框以取消选择它时,“继续”消失了。是否还有一种方法可以在选中任何框时保持显示?

Here is a jsfiddle as well.

$('.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


【解决方案1】:

怎么样

jQuery.fn.fadeBoolToggle = function(bool){
  return bool ? this.fadeIn(1000) : this.fadeOut(1000);
}
$(function() {
  $('.calendar-check').on('click', function() {
    $(this).parents('.product-wrap:first').find('.checkmark-img').toggle(this.checked);
//        $('#next1').toggle($('.calendar-check:checked').length > 0);
    $('#next1').fadeBoolToggle($('.calendar-check:checked').length > 0);
    var prods = [];
    $('.calendar-check:checked').each(function() { prods.push($(this).val()) });
    $("#prods").html("Product"+
                     (prods.length==1?"":"s")+
                     " chosen: "+prods.join(", ")
                    );
  });
});
.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>
<div id="prods">Products chosen</div>
<div class="proceed-btn" id="next1">PROCEED</div>

【讨论】:

  • 我想从复选框输入中获取值并将它们显示在我的框下方。 $('#package2').val();。然后,如果您运行 sn-p 并查看两个框被选中时,再次单击一个,您将看到 onchange 事件使继续按钮消失。所以本质上,如果你决定同时使用这两个包,然后在继续之前删除一个包,按钮就会消失。
  • 关于继续按钮,如果任何框处于活动状态(选中),我希望继续按钮也保持活动状态。不知道该怎么做,以及为复选框输入写值。
  • 一旦点击某些东西,这将始终保持继续按钮显示。
  • 我做了,但是如果您单击一个并激活它,然后再次选中它以取消选择它,该按钮仍然保持活动状态。
  • 试试我更新的代码。假设选择器正确,如果没有选中框,则继续将消失
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2018-07-24
  • 2013-03-21
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
相关资源
最近更新 更多