JavaScript 如何將 querySelectorAll 選取特定元素

1
2
var test = document.querySelectorAll('input[value][type="checkbox"]:not([value=""])');
//get all inputs with the attribute "value" and has the attribute "value" that is not blank.

以下是範例

  • .html

    1
    2
    3
    4
    5
    <input type="checkbox" id="c2" name="c2" value="DE039230952"/>
    <input type="checkbox" id="c2" name="c2" value=""/>
    <input type="checkbox" id="c2" name="c2" />
    <input type="text" id="c2" name="c2" value="DE039230952"/>
    <input type="radio" id=
  • .css

    1
    2
    3
    4
    input{
    display:block;
    margin:20px;
    }
  • .js

    1
    2
    3
    4
    5
    var test = document.querySelectorAll('input[value][type="checkbox"]:not([value=""])');

    for (var i = 0; i < test.length; i++) {
    test[i].disabled = "disabled"
    }

參考

0%