Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算脚本中的分类数_Javascript_Jquery - Fatal编程技术网

Javascript 计算脚本中的分类数

Javascript 计算脚本中的分类数,javascript,jquery,Javascript,Jquery,我有一个函数,它告诉我们是否有值大于63的复选框,而不是show div,否则是hide 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');

我有一个函数,它告诉我们是否有值大于63的复选框,而不是show div,否则是hide 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"
我需要条件语句来说明是否有大于0的复选框,而不是show div,否则是hide div

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

这将满足您的要求

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][]" />

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

$(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;
}

向我们展示其中一个复选框的标记,以便我们了解您的意思:)标记添加到originalIs除了val检查之外,还是替代val检查?替代val检查,考虑使用稍微更符合标准的
数据-
属性表示法,以便
分类法\u计数
变成
数据分类法计数
。然后,您可以使用
$(this).data(“分类计数”)
来获取该值,并且该值已经是一个数字。消息不会出现和消失-我的坏消息。现在试试看。我不习惯键入“分类法”一词,因此我将其拼写为两种不同的方式:pArg-现在,当我单击复选框时,它不会出现您是否已将单击事件处理程序正确分配给该复选框?在浏览器中打开控制台,然后输入
show\u no\u taxonomies()
手动运行该函数。
$(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;
}