javascript - 如何使用 JavaScript 访问下面构造的 HTML 对象
问题描述
我正在尝试限制列表中的选择 - 字符串复选框。但是,我似乎无法在 HTML 中选择对象。我正在对我的对象使用应用程序构建器并尝试实现附加的 JavaScript 代码。
我尝试使用 document.getElementsByClassName()、.querySelector() 和 .getElementsByTag() 来选择我的对象,但没有成功。
我不知道为什么这段代码不起作用。可能是因为我无法正确构建代码或正确使用选择器吗?我对 JavaScript 不是很熟悉,任何帮助或指导将不胜感激。
var checks = document.querySelectorAll(".item");
var max = 2;
for (var i = 0; i < checks.length; i++)
checks[i].onclick = selectiveCheck;
function selectiveCheck(event) {
var checkedChecks = document.querySelectorAll(".item:checked");
if (checkedChecks.length >= max + 1)
return false;
}
<div id="MultiSelectBox" class="ListData SelectBox cbFormSelect" style="z-
index: 1002; left: 109px; top: 43px; width: 107px; height: 151px;
background-color: rgb(255, 255, 255);">
<div class="Body" style="overflow-y: auto;">
<div style="display: table; width: 100%;">
<div style="">
<div class="Item" style="outline: none;">
<input type="checkbox" style="outline: none; margin-left: 0px; margin-
right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_0"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_0">Apple</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_1"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_1">Banana</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_2"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_2">Melon</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_3"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_3">Mango</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_4"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_4">Grapes</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_5"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_5">Peach</label></div>
</div>
</div>
</div>
</div>
预期的结果很简单,脚本不应该允许所有用户选择超过 2 个选项。
解决方案
你做的完全错了。您正在检查“.item:checked”,所以应该有“item”类的复选框,而不是 div。
<div style="">
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_0">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_0">Apple</label>
</div>
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_1">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_1">Banana</label>
</div>
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_2">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_2">Melon</label>
</div>
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_3">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_3">Mango</label>
</div>
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_4">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_4">Grapes</label>
</div>
<div style="outline: none;">
<input class="item" type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_5">
<label for="InsertRecordCheckList_3a4dfdb26cc834_checked_5">Peach</label>
</div>
</div>
休息还可以
推荐阅读
- firefox - Rust 不接受来自本地消息传递的标准输入的输入 - firefox
- javascript - 如何在 Duktape 中实现非阻塞事件循环?
- laravel - Laravel 根据时间戳更新数据库字段
- c - 矩阵乘法不适用于 C 中大于外部的内部维度
- android - 如何增加 FD_SETSIZE 或限制 Android Emulator 引发的描述符数量
- node.js - Docker Nodejs - 将应用程序文件夹映射到主机文件夹 - 应用程序无法启动,找不到模块
- c# - 在 C# 中计算下一个可接受的通知时间
- reactjs - 前一个函数完成后调用一个函数reactjs
- python - 我可以在不使用try语句的情况下捕获python中的异常信息吗?
- coding-style - 我的解决方案有什么问题?帮我改进它或任何其他解决方案?