【问题标题】:doesn't work .buttonset() on jQuery在 jQuery 上不起作用 .buttonset()
【发布时间】:2013-05-04 16:59:28
【问题描述】:

我有许多由 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); 
});

【问题讨论】:

  • 您是否遇到任何错误.. 检查您的控制台
  • 代码是否包含在document.ready中?
  • @bipen with firebug 没有错误
  • doesn't work 的解释不多。你是否包含了 jQuery UI css?
  • @charlietfl 是的,我确定...

标签: javascript jquery wordpress jquery-ui wordpress-theming


【解决方案1】:

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

<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会找到所有以名字check开头的复选框,比如“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();

【讨论】:

  • 第二个解决方案不起作用?你能把生成的html复制给我吗?
  • 你忘了这行 "" 名称必须是 name="check"。生成的 html 应如下所示:。我在谈论第二种解决方案。
  • 生成的代码是匹配的......但仍然不起作用......如果我添加这个代码:&lt;input type="checkbox" id="check1" /&gt;&lt;label for="check1"&gt;B&lt;/label&gt; 它工作得很好......
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
相关资源
最近更新 更多