- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码笔。它的作用是我们可以添加 Add Category
,这将生成一个蓝色框,最初有一排复选框嵌入在类 cars-item js-cars-item
中。然后我们有不同的部分,用户可以在单击 Add Section
时添加多行复选框。这将嵌入到 js-cars-item-sub-items
我能够在 js-cars-item-sub-items
类中实现对复选框的验证。也就是说,如果我在第 2 节中选中第一行中的第一个复选框,当我在第 3 节中单击第二行中的第一个复选框时,它会取消选中第 2 节,并选中第 3 节。此验证仅发生在部分(绿色框)。
现在,我想要实现的是复选框主行(红色框)中的相同行为 - 也就是说,如果我添加另一个类别,我选中第一个类别中的第一个复选框,当我选中相同的复选框时第二类中的复选框,它应该取消选中第一类中的复选框。
谁能帮我解决这个问题?我完全被困住了。
var categoryCount = 1;
$('.js-add-category').click(function() {
categoryCount++;
var categoryContent = `<br><div class="serv-content">
<div class="title-content">
<input type="text" id="name-title-`+categoryCount+`" class="name-title" value="category-`+categoryCount+`">
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-1">
<label for="car-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-1">
<label for="car-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-1">
<label for="car-3"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section 1 </span>
</div>
</div>
<div class="action-btn">
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
</div>
<br>`
$('.main-content').append(categoryContent);
});
var count = 1;
$(document.body).on('click', 'button.js-add-section', function() {
//count++;
let count = $(this).parent().parent().find('.cars-item').length+1;
var sectionContent = `<div class="cars-item js-cars-item-sub-items">
<ul>
<li>
<input type="checkbox" id="car-1-2">
<label for="car-1-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-2">
<label for="car-2-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-2">
<label for="car-3-2"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section`+count+` </span>
</div>
</div>`
$(this).closest('.serv-content').append(sectionContent);
});
$(document.body).on('click', 'button.js-save-section', function() {
// get selected checkboxes's values
});
// validate checkboxes for sub items..
$(".main-content").on('change', '.js-cars-item-sub-items [type="checkbox"]', function () {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.serv-content').find('.js-cars-item-sub-items').each(function () {
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item-sub-items input:checkbox:checked").each(function (index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
// validate checkboxes for main items..
$(".main-content").on('change', '.js-cars-item [type="checkbox"]', function () {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.serv-content').find('.js-cars-item').each(function () {
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item input:checkbox:checked").each(function (index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
.js-cars-item {
box-sizing: content-box;
width: auto;
height: auto;
padding: 10px;
border: 3px solid red;
}
.js-cars-item-sub-items {
box-sizing: content-box;
width: auto;
height: auto;
padding: 10px;
border: 3px solid green;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.serv-content {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
}
.cars.saved {
border-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main-content">
<div class="serv-content">
<div class="title-content">
<input type="text" id="name-title-1" class="name-title" value="category-1">
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-1" checked>
<label for="car-1-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-1" checked>
<label for="car-2-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-1" checked>
<label for="car-3-1"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section 1 </span>
</div>
</div>
<div class="action-btn">
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
</div>
</div>
<br>
</div>
<br> <br>
最佳答案
当您使用语法 $(this).closest('.serv-content').find('.js-cars-item').each()
你只循环调用者复选框的父 .serv-content
类作为对象 this
代表调用者复选框而不是其他 .serv-content
复选框。
因此,为了遍历其他 .serv-content
,我向所有 .serv-content
div 添加了一个 id
您正在创建,当您单击复选框时,我将遍历所有具有类 .serv-content
但不是 id
的 div
来电者的。
首先,我在 $('.js-add-category').click()
from this:
var categoryContent = `<br><div class="serv-content">
to this:
var categoryContent = `<br><div id="serv-` + categoryCount + `" class="serv-content">
然后更改复选框的点击功能 $(".main-content").on('change', '.js-cars-item [type="checkbox"]', function() { });
From this:
$(".main-content").on('change', '.js-cars-item [type="checkbox"]', function () {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.serv-content').find('.js-cars-item').each(function () {
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item input:checkbox:checked").each(function (index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
To This:
$(".main-content").on('change', '.js-cars-item [type="checkbox"]',
function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
var callerId = $(this).closest('.serv-content').attr('id');
$('.main-content').find('.serv-content:not(#' + callerId + ')').each(function() {
$(this).find('.js-cars-item').each(function() {
//Find the checkbox with the same index of clicked checkbox. Change
disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item
input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
这是您的完整代码片段。
var categoryCount = 1;
$('.js-add-category').click(function() {
categoryCount++;
var categoryContent = `<br><div id="serv-` + categoryCount + `" class="serv-content">
<div class="title-content">
<input type="text" id="name-title-` + categoryCount + `" class="name-title" value="category-` + categoryCount + `">
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-1">
<label for="car-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-1">
<label for="car-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-1">
<label for="car-3"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section 1 </span>
</div>
</div>
<div class="action-btn">
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
</div>
<br>`
$('.main-content').append(categoryContent);
});
var count = 1;
$(document.body).on('click', 'button.js-add-section', function() {
//count++;
let count = $(this).parent().parent().find('.cars-item').length + 1;
var sectionContent = `<div class="cars-item js-cars-item-sub-items">
<ul>
<li>
<input type="checkbox" id="car-1-2">
<label for="car-1-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-2">
<label for="car-2-2"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-2">
<label for="car-3-2"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section` + count + ` </span>
</div>
</div>`
$(this).closest('.serv-content').append(sectionContent);
});
$(document.body).on('click', 'button.js-save-section', function() {
// get selected checkboxes's values
});
// validate checkboxes for sub items..
$(".main-content").on('change', '.js-cars-item-sub-items [type="checkbox"]', function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.serv-content').find('.js-cars-item-sub-items').each(function() {
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item-sub-items input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
// validate checkboxes for main items..
$(".main-content").on('change', '.js-cars-item [type="checkbox"]', function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
var callerId = $(this).closest('.serv-content').attr('id');
debugger;
$('.main-content').find('.serv-content:not(#' + callerId + ')').each(function() {
$(this).find('.js-cars-item').each(function() {
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
});
var checkeds = [];
$(this).closest(".serv-content").find(".js-cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
});
.js-cars-item {
box-sizing: content-box;
width: auto;
height: auto;
padding: 10px;
border: 3px solid red;
}
.js-cars-item-sub-items {
box-sizing: content-box;
width: auto;
height: auto;
padding: 10px;
border: 3px solid green;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.serv-content {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
}
.cars.saved {
border-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main-content">
<div class="serv-content">
<div class="title-content">
<input type="text" id="name-title-1" class="name-title" value="category-1">
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-1" checked>
<label for="car-1-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-1" checked>
<label for="car-2-1"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-1" checked>
<label for="car-3-1"><i class="icon-tick"></i></label>
</li>
</ul>
<div class="footer">
<span class="num"> Section 1 </span>
</div>
</div>
<div class="action-btn">
<button type="button" class="js-add-section">Add Section</button>
<button type="button" class="js-save-section">Save</button>
</div>
</div>
<br>
</div>
<br> <br>
关于javascript - 验证在 jquery 中动态生成的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522317/
我是一名优秀的程序员,十分优秀!