如何使用querySelectorAll只有具有特定属性集的元素? [英] How to use querySelectorAll only for elements that have a specific attribute set?

查看:1683
本文介绍了如何使用querySelectorAll只有具有特定属性集的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对所有具有 value 属性集的复选框使用 document.querySelectorAll p>

页面上还有其他没有设置 value 的复选框,每个复选框的值不同。



示例:
< input type =checkboxid =c2name =c2value =DE039230952/>



如何选择只设置了值的复选框?

解决方案

您可以使用 querySelectorAll()

  var test = document.querySelectorAll('input [value] [type =checkbox]:not([value =])'); 

这转换为:


获取属性为value的所有输入,并且属性value不为空。


a href =http://jsfiddle.net/mfm2G/1/ =noreferrer>在此演示中,它会禁用具有非空白值的复选框。


I'm trying to use document.querySelectorAll for all checkboxes that have the value attribute set.

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.

Example: <input type="checkbox" id="c2" name="c2" value="DE039230952"/>

How do I select just those checkboxes that have values set?

解决方案

You can use querySelectorAll() like this:

var test = document.querySelectorAll('input[value][type="checkbox"]:not([value=""])');

This translates to:

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只有具有特定属性集的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆