默认情况下选中一个复选框

时间:2018-10-02 07:58:20

标签: php wordpress

我知道之前曾有人问过这个问题,但我一直在努力选择默认复选框。我希望复选框默认为“已使用肯尼亚”,如下图所示:Selected "Kenyan Used。我尝试过的是$taxonomy= $wpdb->get_results("SELECT term_id FROM par_taxonomy WHERE (taxonomy = 'condition' AND term_taxonomy_id= '181')");。虽然我不是下面的代码如何实现此目的,但我知道这是一个for循环,但是如何修改它以适合我的情况?以下是从中获取分类法的特定代码:

<?php
   if (!empty($modern_filter)){ $counter = 0;
    foreach ($modern_filter as $modern_filter_unit) {
        $counter++;
        $terms = get_terms(array($modern_filter_unit['slug']), $args);
        if (empty($modern_filter_unit['slider']) && $modern_filter_unit['slug'] != 'price') { /*<!--If its not price-->*/
                     //if ($modern_filter_unit['slug'] != 'price') { /* != 'price'<!--If its not price-->*/
            /*First one if ts not image goes on another view*/
            if ($counter == 1 and empty($modern_filter_unit['use_on_car_modern_filter_view_images'])
                    and !$modern_filter_unit['use_on_car_modern_filter_view_images']) {
                if (!empty($terms)) { ?>
<div class="stm-accordion-single-unit <?php echo esc_attr($modern_filter_unit['slug']); ?>">
   <a class="title" data-toggle="collapse"
      href="#<?php echo esc_attr($modern_filter_unit['slug']) ?>" aria-expanded="true">
      <h5><?php esc_html_e($modern_filter_unit['single_name'], 'motors'); ?></h5>
      <span class="minus"></span>
   </a>
   <div class="stm-accordion-content">
      <div class="collapse in content" id="<?php echo esc_attr($modern_filter_unit['slug']); ?>">
         <div class="stm-accordion-content-wrapper">
            <?php foreach ($terms as $term): ?>
            <?php if (!empty($_GET[$modern_filter_unit['slug']]) and $_GET[$modern_filter_unit['slug']] == $term->slug) { ?>
            <script type="text/javascript">
               jQuery(window).load(function () {
                var $ = jQuery;
                $('input[name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"]').click();
                $.uniform.update();
               });
            </script>
            <?php } ?>
            <div class="stm-single-unit">
               <label>
               <input type="checkbox"
                  name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
                  data-name="<?php echo esc_attr($term->name); ?>"
                  />
               <?php echo esc_attr($term->name); ?>
               </label>
            </div>
            <?php endforeach; ?>
         </div>
      </div>
   </div>
</div>
<?php } ?>

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

假设这是您要执行的复选框:添加if语句以确定是否应默认选中该复选框

 <input type="checkbox"
        name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
        data-name="<?php echo esc_attr($term->name); ?>"
        <?php if($condition){?> checked <?php } ?>
 />

将$ condition替换为某些内容,以确定这是否是您默认要选中的复选框。如果要默认选中所有复选框,则只需在输入标签内添加“ checked”,而不添加任何if语句。

答案 1 :(得分:0)

您需要将checked属性添加到您的复选框。您可以使用if语句检查所需的术语。您需要添加以下内容:

<?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?>

所以您的输入代码如下:

<input type="checkbox"
      name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
      data-name="<?php echo esc_attr($term->name); ?>"
      <?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?> />
<?php echo esc_attr($term->name); ?>

希望这会有所帮助!

相关问题