Error while query selector on numeric value

后端 未结 2 1244
忘了有多久
忘了有多久 2020-12-12 01:12

I am trying to set checkbox value for a filter component using query selector (without jquery). If the value for the element is string then query selection is success howeve

相关标签:
2条回答
  • 2020-12-12 01:38

    You might use " for the value of your selector : [key="value"]

    document.querySelector('button').addEventListener('click',function(){
      document.querySelector('input[name="manufacturer"][value="apple"]').checked = true;
      document.querySelector('input[name="storage"][value="16"]').checked = true;
    })
    
    0 讨论(0)
  • 2020-12-12 02:01

    See the specification:

    Attribute values must be CSS identifiers or strings.


    In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

    16 starts with a digit, so it (unlike manufacturer, apple and storage) cannot be an identifier.


    Strings can either be written with double quotes or with single quotes.

    16 is not written with either of those, so it isn't a string.


    You need to quote the number.

    document.querySelector('input[name=storage][value="16"]')
    
    0 讨论(0)
提交回复
热议问题