首页 > 解决方案 > jQuery 如果每个 li == 0 内的每个输入值

问题描述

我正在尝试解决这个问题。我有多个输入,我想比较是否每个人的值 == 0 并且也被检查。

我不知道该怎么做——我几乎花了一天的时间寻找并找到一种方法来做这件事,但不知道如何走得更远。我试图找出是否检查了一个输入并具有该值。

谢谢你的帮助。

$(document).on('change','select, input', function() {
  console.log('input/select has been changed');
  
  var $this = $(this);
  var $inputValue = $this.val();
  
  console.log('input Value is ' + $inputValue);
  
  if ($inputValue == 0 && $this.is(':checked')) {
    console.log('One input is checked and has 0 value');
  }
  
  if ('all first inputs are checked and has 0 value') {
    console.warn('Could U help me?');
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div class="destinations">
    <ul>
      <li>
        <label>
          <input type="checkbox" name="destination" value="0">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" name="destination" value="5">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" name="destination" value="3">
        </label>
      </li>
    </ul>
  </div>
  <div class="languages">
    <ul>
      <li>
        <label>
          <input type="radio" name="language" value="0">
        </label>
      </li>
      <li>
        <label>
          <input type="radio" name="language" value="5">
        </label>
      </li>
      <li>
        <label>
          <input type="radio" name="language" value="3">
        </label>
      </li>
    </ul>
  </div>
  
  <div class="rooms">
    <ul>
      <li>
        <label>
          <input type="checkbox" name="room" value="0">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" name="room" value="5">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" name="room" value="3">
        </label>
      </li>
    </ul>
  </div>
</body>

标签: javascriptjqueryhtml

解决方案


<script>
        $(document).on('change', 'select, input', function () {
            console.log('input/select has been changed');
            checkInputValue();

        });
        function checkInputValue() {
            var count = 0;
            $('input').each(function ( index,value) {
                console.log(value);
                console.log(index);
                if ($(value).val() == 0 && $(value).is(':checked')) {
                    count = count + 1;
                    console.log(count+' input is checked and has 0 value');
                }

            });
            console.log($('input').length);
            if ($('ul').length == count)
                console.log('all first inputs are checked and has 0 value')
        }
    </script>

推荐阅读