javascript - 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>
解决方案
<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>
推荐阅读
- python - 优化参数以在一维数组中查找峰值
- javascript - 如何在 Node.js 中广播实时音频?
- python - 使用 Firebase Admin SDK 和 Python 的身份验证错误
- python - Python:使用现有有序字典中的键制作一个空的字典问题
- vb.net - 如何在 DataGridView 中填充 DataTable
- java - 执行 textView 命令时应用程序崩溃
- r - 在 Shiny 中使用 downloadHandler 使拼凑与 pdf 创建器兼容?
- powershell - 仅使用 powershell/selenium 查找网页上的所有元素
- java - 如何在这些代码行中添加 if-else 语句?
- python - 使用 Selenium/Python 查找按钮元素