2013-10-03 143 views
4

我有一個附有複選框的商店名稱列表。 我有另一個屬於這些商店的分類列表。 當我點擊商店時,它只顯示具有相同價值的分類法。當沒有選中複選框時顯示一條消息

我用這個腳本來做到這一點:

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

}); 

現在我想如果沒有框都被選中

怎麼可能做到這一點在我的分類顯示一條消息?

我想:

$(document).ready(function() { 
    if($('.store_checkbox').find('input[type=checkbox]:checked').length > 0) { 
    $("#hidden_taxon_message").show(); // any one is checked 
} else { 
    $("#hidden_taxon_message").hide(); // none is checked 
    } 
}); 

但不管它只是隱藏了消息,如果有選中或取消選中複選框

我也試過這樣:

$(function() { 

if($('.store_checkbox').not(':checked')){ 
alert("no check boxes"); 
} 
}); 

更新腳本一起

$(document).ready(function() { 
$(".store_checkbox").change(function() { 
    $('div[store_id=' + this.value + ']').toggle(this.checked); 
}).change(); 
if(!$('.store_checkbox').is(':checked')){ 
$("#hidden_taxon_message").show(); // any one is checked 
} else { 
$("#hidden_taxon_message").hide(); // none is checked 
} 
}); 

最新

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

checkIfChecked(); 

}); 

$('.store_checkbox').on('change', function() { 
checkIfChecked(); 
}); 

function checkIfChecked() { 
if ($('.store_checkbox:checked').length == 0) { 
    $("#hidden_taxon_message").show(); // none are checked 
} else { 
    $("#hidden_taxon_message").hide(); // something is selected 
} 
} 

這是我的js文件的樣子。該消息似乎並沒有隱藏。 我把它放在正確的位置?

標記

<h3>Stores Offered In</h3> 
    <ul class="multi-column-checkbox"> 
    <% for store in Store.all %> 
     <li><%= check_box_tag "idea[store_ids][]", store.id, 
@idea.stores.include?(store), :class => "store_checkbox" %> <%= store.name %></li> 
    <% end %> 
    </ul> 
    <br /> 

    <hr /> 

    <h3>Taxonomies Offered In</h3> 
    <% for store in Store.all %> 
    <% if store.has_taxonomies? %> 
    <div store_id='<%= store.id %>'> 
     <h4><%= store.name %></h4> 
      <ul class="multi-column-checkbox"> 
      <% for taxonomy in store.taxonomies %> 
       <li><%= check_box_tag "idea[taxonomy_ids][]", 
taxonomy.id, @idea.taxonomies.include?(taxonomy) %> <%= taxonomy.name %></li> 
      <% end %> 
      </ul> 
    </div> 
    <% end %> 
    <% end %> 
    <div id="hidden_taxon_message"> 
     no checkboxes are selected    
    </div> 
+1

您問題,你的邏輯看起來倒退了。如果你想在選擇0時顯示一條消息,你會不會== 0而不是> 0,或者切換show()/ hide()? – Gregg

+0

它看起來像只測試是否顯示一次消息(文檔準備就緒後)。看起來你會希望每次複選框被更改時執行該測試,對吧?可能還包括你的HTML? – showdev

+0

我的更新問題是否消除了文檔準備好? – anmaree

回答

1

您需要將支票結合的輸入框的變化情況,如下所示:

$('input[type=checkbox]').on('change', function() { 
    if ($('input[type=checkbox]:checked').length > 0) { 
     $("#warning").show(); // any one is checked 
    } else { 
     $("#warning").hide(); // none is checked 
    } 
}); 

因此,當一個新的複選框被選中的代碼會跑。

看到這個搗鼓工作示例:http://jsfiddle.net/KyleMuir/JxSXY/

編輯

對於未選擇任何你需要檢查文檔加載以及對變化,看到以下內容:

$(document).ready(function() { 
    checkIfChecked(); 
}); 

$('input[type=checkbox]').on('change', function() { 
    checkIfChecked(); 
}); 

function checkIfChecked() { 
    if ($('input[type=checkbox]:checked').length == 0) { 
     $("#warning").show(); // none are checked 
    } else { 
     $("#warning").hide(); // something is selected 
    } 
} 

小提琴:http://jsfiddle.net/KyleMuir/JxSXY/2/

希望這會有所幫助。

+0

這是顯示一條消息,當點擊某物時,我正試圖對他做出反應 – anmaree

+0

@anmaree修正:) –

+0

該示例非常好用!我只是很難與我的其他腳本放在一起:'$(「。store_checkbox」)。change(function(){('div [store_id ='+ this.value +']')。 toggle(this.checked); })。change();' – anmaree

0

假設.store_checkbox是分配給所有的複選框一類,你的邏輯 需要看起來像這樣:

if($('.store_checkbox:checked').length == 0) { 
    $("#hidden_taxon_message").show(); // any one is checked 
} else { 
    $("#hidden_taxon_message").hide(); // none is checked 
}; 

或者,你可以使用一些不同的語法,更像是你的第二個例子。有趣的是,the opposite of jQuery's .is() method is not .not(), it is !.is()

if(!$('.store_checkbox').is(':checked')){ 
    alert("no check boxes"); 
} 

爲了把它放在一起,你可以做這樣的事情(感謝@ gibbeirsh的編輯爲的jsfiddle):基於

$(document).ready(function() { 
    $('.store_checkbox').click(function() { 
     msgconfig(); 
    }); 
    msgconfig(); 
}); 
function msgconfig(){ 
    if ($('.store_checkbox:checked').length == 0) { 
     $("#hidden_taxon_message").show(); 
    }else{ 
     $("#hidden_taxon_message").hide(); 
    } 
} 

jsFiddle

+0

此代碼似乎完全覆蓋我的其他代碼 – anmaree

+0

哎呦。我的代碼中有一個額外的支架,可能完全破壞了您的代碼。我剛剛編輯來解決這個問題。 – Divey

+0

第二個代碼在頁面加載時很好地工作,但是當我在頁面上時取消選擇 – anmaree

相關問題