作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有所有工作日的复选框。用户只需取消选中他/她不想检查的日期即可。我将天数以逗号分隔的字符串形式保存在数据库中:1,2,4,5
。我从数据库正确地取回了字符串。
问题
我无法取消选中用户之前取消选中的日期。或者我说我只想显示已检查的日期,并希望取消选中未通过使用检查的日期。
到目前为止已经尝试过什么?
我使用 split
从字符串创建了一个数组。
获取数组后,我使用 For 循环取消选中该复选框,但它不起作用。
但是我的“我的星期一”复选框被取消选中。
var working_day_string = '1,2,4,5';
var working_day_array = working_day_string.split(',');
for (i=0;i<working_day_array.length;i++)
{
if(working_day_array[i] != '1'){
$('#monday').prop('checked', false);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-10">
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="monday" name="working_days[]" value="1" checked>
<label for="monday"> Monday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="tuesday" name="working_days[]" value="2" checked>
<label for="tuesday"> Tuesday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="wednesday" name="working_days[]" value="3" checked>
<label for="wednesday"> Wednesday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="thursday" name="working_days[]" value="4" checked>
<label for="thursday"> Thursday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="friday" name="working_days[]" value="5" checked>
<label for="friday"> Friday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="saturday" name="working_days[]" value="6" checked>
<label for="saturday"> Saturday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="sunday" name="working_days[]" value="7" checked>
<label for="sunday"> Sunday </label>
</div>
</div>
最佳答案
尝试以下操作
var working_day_string = '1,2,4,5';
var working_day_array = working_day_string.split(',');
$('input[type="checkbox"]').prop('checked', false); //reset all checkboxes
$.each(working_day_array, function(i, day) {
$('input[value="' + day + '"]').prop('checked', true); //check the box from the array, note: you need to add a class to your checkbox group to only select the checkboxes, right now it selects all input elements that have the values in the array
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-10">
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="monday" name="working_days[]" value="1" checked>
<label for="monday"> Monday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="tuesday" name="working_days[]" value="2" checked>
<label for="tuesday"> Tuesday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="wednesday" name="working_days[]" value="3" checked>
<label for="wednesday"> Wednesday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="thursday" name="working_days[]" value="4" checked>
<label for="thursday"> Thursday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="friday" name="working_days[]" value="5" checked>
<label for="friday"> Friday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="saturday" name="working_days[]" value="6" checked>
<label for="saturday"> Saturday </label>
</div>
<div class="checkbox checkbox-custom checkbox-inline">
<input type="checkbox" id="sunday" name="working_days[]" value="7" checked>
<label for="sunday"> Sunday </label>
</div>
</div>
关于jquery - 在 for 循环中循环数组时取消选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44794053/
我是一名优秀的程序员,十分优秀!