問題描述
我正在嘗試對所有設置了 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 用于具有特定屬性集的元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!