0

我有一个函数,如果存在值大于 63 的复选框,则显示 div,否则隐藏 div。

function show_no_taxonomies() {
 if ($('.store_checkbox:checked').val() > 63){
    $("#hidden_taxon_message").show();
    $("#hidden_taxon_message").text('This store does not have any texonomies');
 }else {
    $("#hidden_taxon_message").hide(); // something is selected
   }
}

我需要重新定义这个条件 if 语句来计算分类。我在所有这些复选框上都附加了这个标签:

taxonomies_count="0"

我需要条件语句来说明是否存在taxonomies_count大于 0 的复选框,而不是显示 div,否则隐藏 div。

<input id="idea_store_ids_" class="store_checkbox" type="checkbox" value="124"   
taxonomies_count="0" name="idea[store_ids][]"></input>
4

2 回答 2

0

这将满足您的要求...

function show_no_taxonomies() {
    var taxonomiesCount = false;
    $('.store_checkbox:checked').each(function() {
        if ($(this).attr("taxonomies_count") > 0) {
            taxonomiesCount = true;
            return;
        }
    });
    if (!taxonomiesCount){
        $("#hidden_taxon_message").show();
        $("#hidden_taxon_message").text('This store does not have any taxonomies');
    }else {
        $("#hidden_taxon_message").hide(); // something is selected
    }
}

但是,我建议使用数据属性,而不是自定义属性。像这样...

<input id="idea_store_ids_" class="store_checkbox" type="checkbox" value="124" data-taxonomies-count="0" name="idea[store_ids][]" />

脚本将是...

function show_no_taxonomies() {
    var taxonomiesCount = false;
    $('.store_checkbox:checked').each(function() {
        if ($(this).data("taxonomies-count") > 0) {
            taxonomiesCount = true;
            return;
        }
    });
    if (!taxonomiesCount){
        $("#hidden_taxon_message").show();
        $("#hidden_taxon_message").text('This store does not have any taxonomies');
    }else {
        $("#hidden_taxon_message").hide(); // something is selected
    }
}
于 2013-10-03T21:30:15.973 回答
0

我通过制作另外 2 个大图片函数在逻辑上简化我的代码来解决这个问题。然后,将这些函数调用到我更大的函数中。

$(document).ready(function() {
$(".store_checkbox").change(function () {
    $('div[store_id=' + this.value + ']').toggle(this.checked);
    show_no_store_message();
}).change();
show_no_store_message();
});

function show_no_store_message() {
if (!is_store_selected()) {
    $("#hidden_taxon_message").show(); // none are checked
    $("#hidden_taxon_message").text('Please select store before selecting taxonomies');
} else if (is_store_selected()  && !do_any_stores_have_taxonomies() ) {
    $("#hidden_taxon_message").show(); // none are checked
    $("#hidden_taxon_message").text('None of the stores you selected have taxonomies');

} else {
    $("#hidden_taxon_message").hide(); // something is selected
}
}

// returns true if any store is selected
function is_store_selected(){
return ($('.store_checkbox:checked').length > 0);
}

// Returns true if any store selected AND store has taxonomiess
function do_any_stores_have_taxonomies(){
$('.store_checkbox:checked').each(function() {
    if ($(this).attr("taxonomies_count") > 0) {
     return true;   
    }
});
return false;
}
于 2013-10-04T13:59:37.993 回答