简体   繁体   English

jQuery数据属性选择器问题

[英]jQuery data attribute selector issue

I am trying to show some span`s with data attribute base on select options available. 我正在尝试根据可用的选择选项显示一些具有数据属性的span`s。

I am using the below code 我正在使用以下代码

  $("#pa_varsta option").each(function(i){
        var marime =  $(this).val();
        $('.tawcvs-swatches [data-value=' + marime + ']').show();
    });

But i get the error 但是我得到了错误

jquery.js?ver=1.12.4:2 Uncaught Error: Syntax error, unrecognized expression: .tawcvs-swatches [data-value=]

I am using Wordpress. 我正在使用Wordpress。

Any ideea, maybe regarding the jquery version? 任何想法,也许关于jQuery版本?

.val() is only for input elements not for an <option> element. .val()仅适用于输入元素,不适用于 <option>元素。 Use .text() instead. 使用 .text()代替。

$("#pa_varsta option").each(function(i){
  var marime =  $(this).val();
  $('.tawcvs-swatches [data-value="' + marime + '"]').show();
});

It is safer to surround the attribute's value with a quote. 用引号将属性的值引起来是更安全的。 Because sometimes, it will break the selector. 因为有时会破坏选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM