У меня есть функция, которая говорит, если есть флажки со значением больше 63, чем show 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
}
}
Мне нужно переопределить это условное утверждение, чтобы подсчитать таксономии. У меня есть этот тег, прикрепленный ко всем этим флажкам:
taxonomies_count="0"
Мне нужно условное утверждение, чтобы сказать, есть ли флажки с taxonomies_count больше 0, чем show div, иначе скрыть div.
<input id="idea_store_ids_" class="store_checkbox" type="checkbox" value="124"
taxonomies_count="0" name="idea[store_ids][]"></input>
Я решил это, логически упростив свой код, сделав еще две большие функции изображения. Затем вызов этих функций в мою большую функцию.
$(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;
}
Это сделает то, что вы просили...
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
}
}
Однако я бы рекомендовал использовать атрибут data, а не специальный атрибут. Как это...
<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
}
}