if/else jQuery 使用自定义属性

if/else jQuery using custom attribute

本文关键字:自定义属性 jQuery else if      更新时间:2023-09-26

我正在尝试使用自定义属性在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,我只想显示数据 ID 为 0 的 moddellselect,如果标记值不是 0,则我想显示所有模型选择。

您需要为此

使用 .filter

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

(请记住,Id 应该是唯一的 - 看起来你的不是)