0

在下面的脚本中,我正在尝试使用类“cb-checkURL”评估所有复选框项目,如果选中,我想通过 jQuery show() 将最近的“.wl-options”div 设置为 display:block。

但是,我错过了一些东西, .each 没有做这项工作。有任何想法吗?

jQuery(document).ready(function(){
    jQuery.each( ".cb-checkURL", function() {
        if(jQuery(this).attr("checked")){
            jQuery(this).parent().next(".wl-options").show();
        }
        else
        {
            jQuery(this).parent().next(".wl-options").hide();
        }
    });

    jQuery(".cb-checkURL").click(
        function(){
            jQuery(this).parent().next(".wl-options").toggle();
        });
})';

HTML 源

<label class="wl-active">
    <input class="cb-checkURL" type="checkbox" name="wl[item-2][location][is_url]" checked="checked">&nbsp;URL
</label>
<div class="wl-options" style="clear:both;max-width:400px;display:none">
    <div class="cb-urls">
        <label title="Enter one url slug per line" for="wl[item-2][url][urls]"></label>
        <textarea name="wl[item-2][url][urls]" id="wl[item-2][url][urls]"></textarea>
    </div>
</div>
4

3 回答 3

2

它可能更简单:

jQuery(document).ready(function(){
    var handler = function() {       
        jQuery(this).parent().next(".wl-options").toggle(jQuery(this).is(':checked'));        
    };

    jQuery(".cb-checkURL").click(handler).each(handler);          
});

JSFiddle:http: //jsfiddle.net/jA4wS/

于 2013-02-14T14:53:00.797 回答
0
  jQuery.each( ".cb-checkURL", function() {

你要

$(".cb-checkURL").each(function(index) {
    // do something on each .cb-checkURL
  });
于 2013-02-14T14:49:42.813 回答
0

您没有在 DOM 中选择任何元素。“$.each() 函数与 $(selector).each() 不同,后者用于专门迭代 jQuery 对象。”,http: //api.jquery.com/jQuery.each / 尝试:

jQuery(".cb-checkURL").each(function( index ) {})
于 2013-02-14T14:46:51.480 回答