How to use querySelectorAll only for elements that have a specific attribute set?(如何仅将 querySelectorAll 用于具有特定属性集的元素?)
问题描述
我正在尝试对所有设置了 value
属性的复选框使用 document.querySelectorAll
.
I'm trying to use document.querySelectorAll
for all checkboxes that have the value
attribute set.
页面上还有其他复选框没有设置value
,每个checkbox的值都不一样.id 和名称不是唯一的.
There are other checkboxes on the page that do not have value
set, and the value is different for each checkbox. The ids and names are not unique though.
示例:<input type="checkbox" id="c2" name="c2" value="DE039230952"/>
如何只选择那些设置了值的复选框?
How do I select just those checkboxes that have values set?
推荐答案
你可以像这样使用querySelectorAll()
:
var test = document.querySelectorAll('input[value][type="checkbox"]:not([value=""])');
这转化为:
获取属性为value"且属性value"不为空的所有输入.
get all inputs with the attribute "value" and has the attribute "value" that is not blank.
在这个演示中,它禁用了非空值的复选框.
In this demo, it disables the checkbox with a non-blank value.
这篇关于如何仅将 querySelectorAll 用于具有特定属性集的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何仅将 querySelectorAll 用于具有特定属性集的元


基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01