【问题标题】:Echoed radio buttons won't check回显单选按钮不会检查
【发布时间】:2013-09-11 05:29:59
【问题描述】:

原谅我的php,我对它很陌生。

我在 Wordpress 中创建了一个类别循环,以根据类别输出不同的标记(因此类别 1 的元素将获得一个唯一的类等)。我有它,所以它与用户可以选择的单选按钮相呼应。一切正常,除了单选按钮不检查。我可以取消选中它们,但我无法选中它们。静态 html 版本运行良好。

静态(工作)版本:

<input id="type-1" name="s1" type="radio" class="type-1">
<label for="type-1" class="label-1">&nbsp;2010 </label>

<input id="type-3" name="s1" type="radio" class="type-3">
<label for="type-3" class="label-3">&nbsp;2011 <span>.</span> </label>

<input id="type-4" name="s1" type="radio" class="type-4">
<label for="type-4" class="label-4">&nbsp;2012 <span>.</span> </label>

<input id="type-5" name="s1" type="radio" class="type-5">
<label for="type-5" class="label-5">&nbsp;2013 <span>.</span> </label>

动态(不工作)版本:

    <?php
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) { 
        $args=array(
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo "<input id=\"type-{$category->term_id}\" name=\"s1\" type=\"radio\" class=\"type-{$category->term_id}\"><label for=\"type-{$category->term_id}\" class=\"label-{$category->term_id}\">&nbsp;{$category->name}<span>.</span> </label>";
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

更新:想通了。我是个白痴,它与上面的代码无关。这是一个愚蠢的 CSS 问题。

【问题讨论】:

    标签: php wordpress button radio


    【解决方案1】:

    你的输出中缺少花括号,你应该这样写:

    echo "<input id=\"type-{$category->term_id}\" ......";
    

    查看此链接了解详情:

    http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

    或者,您可以使用字符串连接:

    echo "<input id=\"type-" . $category->term_id . "\" ......";
    

    或者你可以使用我个人喜欢的sprintf() (http://php.net/manual/en/function.sprintf.php)

    【讨论】:

    • 感谢您的帮助。我最初使用了连接,但结果相同。我添加了花括号,但没有区别。这很奇怪,因为在查看输出的 HTML 时,最终代码与静态版本相同。只有按钮不会检查。
    猜你喜欢
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2017-12-31
    • 2020-10-05
    • 1970-01-01
    • 2019-05-29
    • 2011-09-30
    • 2019-01-15
    相关资源
    最近更新 更多