Select inputs that doesn't have value attribute with querySelector

后端 未结 2 1816
走了就别回头了
走了就别回头了 2021-01-21 08:55

I\'m using:

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

But it\'s not selecting inputs that doesn\'t have the attribute value

相关标签:
2条回答
  • 2021-01-21 09:23

    Try

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

    This should return all the input that don't have the value attribute.

    0 讨论(0)
  • 2021-01-21 09:40

    You can use this:

    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 讨论(0)
提交回复
热议问题