1

我有许多由 wordpress 初始化的 :checkbox 元素。现在我将 .buttonset() 函数设置为 #format 但这不起作用......就像这个示例:http: //jqueryui.com/button/#checkbox

HTML:

<div id="format">
    <?php
     $categories = get_categories();
     foreach ($categories as $category) { ?>
     <input type="checkbox" name="check" value="<?php echo $category->cat_ID; ?>">
     <label><?php echo $category->cat_name;?></label><?php } ?>
</div>

JS:

$('#format').buttonset();
$('input[type=checkbox]').removeClass('ui-helper-hidden-accessible');

$(':checkbox[name=check]').each(function( i ){
    var nameID = 'check'+ (i+1);
    this.id = nameID;
    $(this).next('label').prop('for', nameID); 
});
4

1 回答 1

0

您所有的复选框都具有相同的名称“检查”。尝试这样做:

<div id="format">
    <?php
     $i = 0;
     $categories = get_categories();
     foreach ($categories as $category) { ?>
     <input type="checkbox" name="check<?php echo $i ?>" value="<?php echo $category->cat_ID; ?>">
     <label><?php echo $category->cat_name;?></label>
     <?php 
       $i++;
       } 
      ?>
</div>

然后在你的js中:

$(':checkbox[name^=check]').each(function( i ){
    var nameID = 'check'+ (i+1);
    this.id = nameID;
    $(this).next('label').prop('for', nameID); 
});

然后 jQuery 会找到所有以名称检查开头的复选框,例如“check1”、“checkblabla”等...

还有“$('#format').buttonset();” 需要在“for”更改之后出现。字体:

http://api.jquery.com/attribute-starts-with-selector/

编辑:

我认为你真的不需要用js改变for,你只能用php来做,然后:

<div id="format">
    <?php
     $i = 0;
     $categories = get_categories();
     foreach ($categories as $category) { ?>
     <input type="checkbox" name="check<?php echo $i ?>" value="<?php echo $category->cat_ID; ?>">
     <label for="check<?php echo $i ?>"><?php echo $category->cat_name;?></label>
     <?php 
       $i++;
       } 
      ?>
</div>

然后在你的js中:

$( "#format" ).buttonset();
于 2013-05-04T17:12:07.320 回答