if / else jQuery使用自定义属性

时间:2016-03-11 15:39:25

标签: javascript jquery jquery-data

我正在尝试使用自定义属性在jquery中执行if / else。我将属性命名为“data-id”,因为我希望我可以使用jquery的.data(),但不知何故它不起作用。

这是我上次尝试的内容:

 if($("#marke").val() == '0'){
    $("#modellselect").attr("data-id").not('0').hide();
 } else{
    $("#modellselect").attr("data-id").show();
 } 

因此,如果市场价值为0,我想只显示带有data-id 0的moddellselect,如果市场价值不是0,我想显示所有的modellselect。

1 个答案:

答案 0 :(得分:3)

您需要使用.filter代替

if($("#marke").val() == '0'){
    $("#modellselect").filter(function(){
        return $(this).data('id') != '0'
    }).hide()
 } else{
    $("#modellselect[data-id]").show();
 } 

(请记住,Id&s应该是独一无二的 - 看起来不像你的那样)