問題描述
我正在嘗試對(duì)所有設(shè)置了 value
屬性的復(fù)選框使用 document.querySelectorAll
.
I'm trying to use document.querySelectorAll
for all checkboxes that have the value
attribute set.
頁(yè)面上還有其他復(fù)選框沒有設(shè)置value
,每個(gè)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"/>
如何只選擇那些設(shè)置了值的復(fù)選框?
How do I select just those checkboxes that have values set?
推薦答案
你可以像這樣使用querySelectorAll()
:
var test = document.querySelectorAll('input[value][type="checkbox"]:not([value=""])');
這轉(zhuǎn)化為:
獲取屬性為value"且屬性value"不為空的所有輸入.
get all inputs with the attribute "value" and has the attribute "value" that is not blank.
在這個(gè)演示中,它禁用了非空值的復(fù)選框.
In this demo, it disables the checkbox with a non-blank value.
這篇關(guān)于如何僅將 querySelectorAll 用于具有特定屬性集的元素?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!