gpt4 book ai didi

javascript - 如何计算 View 中打开的对象上的复选框数量?

转载 作者:行者123 更新时间:2023-12-03 00:28:21 25 4
gpt4 key购买 nike

我正在尝试计算我所在的当前面板上复选框元素的数量。发生的情况是我在 4 个面板上有复选框,每个面板上都有复选框。一次只能打开一个面板。当我对复选框进行计数时,我得到的是应用程序中所有复选框的总数,而不是我显示的面板上的数字。

我尝试专门告诉代码这是打开的窗口 var a = angular.element(document.querySelector('[widget-id="popup-1"] div')).scope().$parent.me.visible
但没有效果,

我不知道如何告诉代码这是打开的面板,仅计算其上的复选框。

我正在尝试计算框的数量,然后比较选中的框,如果 num = 选中,那么我将继续。

$scope.checkNum = function() {
// var a = angular.element(document.querySelector('[widget-id="popup-1"]
div')).scope().$parent.me.visible
var inputs = document.getElementsByTagName("input");
//or document.forms[0].elements;
var cbs = [];
//will contain all checkboxes
var checked = [];
//will contain all checked checkboxes
for (var i = 0; i < inputs.length; i++) {

if (inputs[i].type == "checkbox") {
cbs.push(inputs[i]);
if (inputs[i].checked) {
checked.push(inputs[i]);
}

}
}
var nbCbs = cbs.length;
//number of checkboxes
var nbChecked = checked.length;
//number of checked checkboxes
alert("# checkboxes = " + nbCbs + "\n" + "# checked checkboxes = " +
nbChecked);
console.log(" # checked checkboxes = " + nbChecked);
}

最佳答案

假设您当前的面板如下所示

var test = document.getElementById("dv_Test");
get_CheckboxCount(test);
function get_CheckboxCount(element) {
var check = element.querySelectorAll("input[type=checkbox]");
console.log(check.length);
}
<div id="dv_Test">
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>

<div id="dv_Test2">
<input type="checkbox" />
</div>

关于javascript - 如何计算 View 中打开的对象上的复选框数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53983949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com