如何在wordpress小部件窗体中添加单选按钮

时间:2010-06-24 18:19:19

标签: wordpress

如何在wordpress小部件窗体中添加单选按钮?我已经能够添加保存的输入字段并且工作正常。但是我遇到了无线电按钮问题。任何人?

这是我输入字段的代码:

<p><label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Video ID:'); ?> 
    <input class="widefat" id="<?php echo $this->get_field_id('id');  ?>" 
    name="<?php echo $this->get_field_name('id'); ?>" 
    type="text" value="<?php echo $id; ?>" /></label></p>

这是我的单选按钮

<input type="radio" name="video_size" value="small"> Small<br>
    <input type="radio" name="video_size" value="full" checked> Full 

4 个答案:

答案 0 :(得分:1)

创建每个单选按钮并确保它们位于同一“组”下。以下是您想要的基本HTML版本。

<input type="radio" name="group1" value="small" checked>Small<br />
<input type="radio" name="group1" value="full"> Full<br />

记下name="group1" - 这将两个选项组合在一起,以便您可以选择其中一个。加载单选按钮时,checked标记默认选择。

WordPress版本需要一些逻辑来检查用户选择了什么。您需要检查是选择了small还是full,并相应地补充了checked属性。

例如

if($size == 'Full')
{
     echo '<input type="radio" name="group1" value="small">Small<br />' . "\n";
     echo '<input type="radio" name="group1" value="full" checked> Full<br />' . "\n";
     //This will out put the radio buttons with Full chosen if the user selected it previously.
}
else
{
     echo '<input type="radio" name="group1" value="small" checked>Small<br />' . "\n";
     echo '<input type="radio" name="group1" value="full"> Full<br />' . "\n";
     //This will out put the radio buttons with Small selected as default/user selected.
}

答案 1 :(得分:0)

我尝试使用此功能但它确实保存了我的选择.. 我想我不会在这个脚本中得到var $ size的来源?

我的问题是单选按钮不会保持检查..我尝试使用 if / else解决方案..但只有一个选项保持检查.. (我在逻辑上知道这很奇怪)

我的代码是我正在构建的小部件(wp小部件)的一部分......

这是我的代码:

        <!-- Your Name: Text Input -->
    <?php if( $visiblecategories == showempty ) { ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" checked /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" /> Hide Empty<br />            
    </p>
    <?php } else { ?>
    <?php echo $visiblecategories; ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'visiblecategories' ); ?>"><?php _e('Show Empty Category:', 'badcategory'); ?></label><br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="showempty" style="" /> Show Empty<br />
        <input type="radio" id="<?php echo $this->get_field_id( 'visiblecategories' ); ?>" name="<?php echo $this->get_field_name( 'visiblecategories' ); ?>" value="hideempty" style="" checked /> Hide Empty<br />            
    </p>
    <?php } ?>

答案 2 :(得分:0)

对属于同一组的所有单选按钮使用相同的名称,并使用它来访问该值。

例如,如果您有一个名为“gender”的广播组,其值为“male”和“female”,则对get_field_name以外的所有get_field *调用使用male / female。将“gender”用于get_field_name。

在你的widget()和update()代码中,只使用'gender' - 这将是男性或女性的值。

答案 3 :(得分:0)

对于任何挣扎于javascript问题的人,检查错误的检查,请确保HTML不具有空checked=""属性。仅在选定的单选按钮上添加属性。

此代码适用于具有单选按钮而没有if语句的小部件:

function form( $instance ) {

    /* Option carrier is 'ecw_column' */
    $ecw_column = isset( $instance['ecw_column'] ) ? $instance['ecw_column'] : 'ecw_column_none';

    echo '<p>';

    $value = 'ecw_column_1';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column start') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_2';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Breakpoint') .'</label>';
    echo '<br/>';

    $value = 'ecw_column_3';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Column end') .'</label>';
    echo '<br/>';

    /* Default value */
    $value = 'ecw_column_none';

    echo '<input value="'. $value .'" class="widefat" id="'. $this->get_field_id($value) .'" name="'. $this->get_field_name('ecw_column') .'" type="radio"'. ($ecw_column == $value ? ' checked="checked"' : '') .' />';
    echo '<label for="'. $this->get_field_id($value) .'">'. __('Current') .'</label>';

    echo '</p>';
}
相关问题