Javascript 检查数组jquery的一个元素的属性

Javascript 检查数组jquery的一个元素的属性,javascript,jquery,keyup,Javascript,Jquery,Keyup,当选中输入字段时,我打开了导航的两个主要元素。 只有当两个人工导航元件中的一个打开时,我才尝试在键盘启动esc上触发关闭导航功能: $(document).on('keyup', this.onPressEsc.bind(this)); onPressEsc(e) { if (e.keyCode === 27 && this.$body.find(selectors.input).prop('checked') === true) { this.clos

当选中输入字段时,我打开了导航的两个主要元素。 只有当两个人工导航元件中的一个打开时,我才尝试在键盘启动esc上触发关闭导航功能:

$(document).on('keyup', this.onPressEsc.bind(this));

onPressEsc(e) {
    if (e.keyCode === 27 && this.$body.find(selectors.input).prop('checked') === true) {
        this.closeMainNav();
        this.removeOverlay();
    }
}
对于当前的实现,只检查数组的第一个元素,是否有任何方法来验证这两个元素

onPressEsc(e) {
    if (e.keyCode === 27 && this.$body.find(selectors.input).prop('checked').length === 2) {
        this.closeMainNav();
        this.removeOverlay();
    }
}
.prop方法只获取第一个元素的属性值 在匹配的集合中

所以,您使用的道具不正确。只有当两个元素都被选中时,第一个元素才会返回true

要选择与选择器匹配的所有元素,请输入选中的元素(假设该字符串已定义选择器),添加:checked,然后测试长度:

onPressEsc(e) {
    if (e.keyCode === 27 && this.$body.find(selectors.input + ":checked").length == 2) {
        this.closeMainNav();
        this.removeOverlay();
    }
}

.findselectors.input+:checked.length应该这样做。请删除此。$body.findselectors.input.prop'checked'==true,因为它完全没有用处。..is':checked'给出与prop'checked'相同的结果,只有数组的第一个元素。findselectors.input已验证OK,然后使用.findselectors.input.filter':checked.length您真的应该包含更多的代码,包括您的HTML,以便我们能够准确地看到您正在尝试执行的操作。您没有以正确的方式使用prop。.prop方法只获取匹配集中第一个元素的属性值。同意,先生,我将进行更改Hanks@SeanKendle,是否有方法检查数组中所有元素的属性?我将其作为答案发布。selectors.input真的是一个带有正确选择器的字符串吗?