2014-10-31 113 views
1

选择输入我使用:不具有价值属性与querySelector

document.querySelectorAll('input[value=""]') 

但它不选择不具有属性value输入。
如何选择所有没有值''且没有属性值的输入?

我想这样做没有jQuery和没有任何额外的功能,这可能只有使用querySelectorAll

+0

这是一个有点混乱。你想选择那些有价值的投入,或者那些没有价值的投入? – LcSalazar 2014-10-31 19:38:54

回答

4

尝试

document.querySelectorAll('input:not([value])')

这将返回所有input没有了value属性。

+0

这也是我的第一个想法,只导致:'未捕获的SyntaxError:未能在'Document'上执行'querySelectorAll':'input:not(“[value]”)'不是有效的选择器。“我* *引用了传递给':not()''的'[[value]“'选择器)。 – 2014-10-31 19:39:20

+0

是的,不应该引用它。 – jsfrocha 2014-10-31 19:39:49

+1

那么:[生活和学习](http://jsfiddle.net/davidThomas/dzrbvto9/),我想。 +1 – 2014-10-31 19:41:55

2

您可以使用此:

document.querySelectorAll('input:not([value]):not([value=""])'); 

get all inputs that doesn't have attribute "value" and the attribute "value" is not blank

var test = document.querySelectorAll('input:not([value]):not([value=""])'); 
 
for (var i = 0; i < test.length; ++i) { 
 
    test[i].style.borderColor = "red"; 
 
}
<input type="text" /> 
 
<input type="text" value="2" /> 
 
<input type="text" /> 
 
<input type="text" value="" /> 
 
<input type="text" />

+0

获取所有* text *''元素(未指定限制),其'value'属性设置为空字符串,* not *''元素*没有'value'属性*(这是而不是问题的要求)。 – 2014-10-31 19:43:01

+0

hmm检查更新答案@DavidThomas。 Thx反馈 – 2014-10-31 19:44:18