我想停止一个函数,以便它可以在一页上多次运行。如果单击复选框,我希望清除一些单选按钮。它应该与无限数量的产品一起使用。我在javascript上表现最差,所以我希望我能得到答案?

样本:


$('.product .question-input').change(function() {
  if ($(this).is(':checked')) { //radio is now checked
    $('.product .question-checkbox').prop('checked', false);
  }
  return false;
});

$('.product .question-checkbox').change(function() {
  if ($(this).is(':checked')) {
    $('.product .question-input').prop('checked', false);
  }
  return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product">
  <div class="form-inline justify-content-center">
    <div class="container text-center">
      <div class="product-title">Adobe</div>
    </div>
    <div class="form-group">
      <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">1
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-1" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">2
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-2" value="2"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">3
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-3" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">4
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-4" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">5
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-5" value="1"></label></div>
    </div>

  </div>
  <div class="form-group form-check text-center the-checkbox">
    <input type="checkbox" name="check-1" class="form-check-input question-checkbox" id="check-1">
    <label class="form-check-label" for="check-1">I don't use this product for work</label>
  </div>
</div>
</div>

<div class="product">
  <div class="form-inline justify-content-center">
    <div class="container text-center">
      <div class="product-title">Mocups</div>
    </div>
    <div class="form-group">
      <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">1
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-1" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">2
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-2" value="2"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">3
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-3" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">4
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-4" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">5
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-5" value="1"></label></div>
    </div>

  </div>
  <div class="form-group form-check text-center the-checkbox">
    <input type="checkbox" name="check-1" class="form-check-input question-checkbox" id="check-1">
    <label class="form-check-label" for="check-1">I don't use this product for work</label>
  </div>
</div>
</div>

最佳答案

通过传递对处理程序的引用,而不是直接传递处理程序函数:

function handler() {
  if ($(this).is(':checked')) {
    $('.product .question-input').prop('checked', false);
  }
  return false;
}

$('.product .question-input').change(handler);

$('.product .question-checkbox').change(handler);

09-27 09:46