2010-06-24 80 views
2

如何在我的wordpress小部件表单中添加单选按钮?我已经能够添加输入字段,保存并正常工作。但是我对radiobuttons有麻烦。任何人?如何在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 

回答

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的版本将需要一些逻辑来检查用户所做的选择。您需要检查是选择了小还是满,并且相应地补充了检查的属性。

例如

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. 
} 
+0

感谢,那great..I不能得到它保存到数据库中,虽然,任何想法? – vick 2010-06-24 18:47:43

+0

嗯..在我的示例中,选择将被存储到$ _POST ['group1'],因此您可以尝试检查该变量。还要看看处理单选按钮选择的PHP表单的真实世界示例:http://apptools.com/phptools/forms/forms3。PHP – hsatterwhite 2010-06-24 19:00:38

0

我尝试使用它,但是doesent救我的选择.. 我想我不明白其中变量$大小与此脚本来的?

我的问题是,单选按钮不会保持选中状态..我试着用 的的if/else解决方案..但只有一个seletion保持选中状态.. (我知道logicly五味)

我的代码是一个小部件我建立(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 } ?> 
0

对属于同一组的所有单选按钮使用相同的名称,并用它来访问该值。例如,如果您有一个名为'gender'的值为'male'和'female'的广播组,则使用男性/女性进行除get_field_name之外的所有get_field *调用。使用'性别'作为get_field_name。

在你的widget()和update()代码中仅使用“性别” - 这将具有男性或女性作为值。

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>'; 
} 
相关问题