多年来,许多人似乎都遇到了一个共同的问题,我正在努力解决这个问题。在创建小部件时,我需要一系列单选按钮。使用下面的代码,我可以创建并显示单选按钮,但它似乎并没有将值保存到数据库中,当我点击保存按钮时,所有显示为未选中,并且小部件前端没有返回任何内容。
我相信这就是单选按钮本身的声明方式,但确实在与这种编码水平作斗争。这和$this->get_field_id(\'\') 标签未正确声明?我尝试了许多不同的排列,但没有成功。
我很感激你能提供的任何帮助。
// 1
function Widget_case_study() {
parent::WP_Widget(false, $name = __(\'Radio buttons\', \'radio_buttons\') );
}
// 2
function form($instance) {
$radio_buttons = esc_attr($instance[\'radio_buttons\']);
?>
<p>
<label for="<?php echo $this->get_field_id(\'text_area\'); ?>">
<?php echo(\'Radio buttons\'); ?>
</label><br>
<label for="<?php echo $this->get_field_id(\'radio_option_1\'); ?>">
<?php _e(\'Option 1:\'.$radio_buttons); ?>
<input class="" id="<?php echo $this->get_field_id(\'radio_option_1\'); ?>" name="<?php echo $this->get_field_id(\'radio_buttons\'); ?>" type="radio" value="radio_option_1" <?php if($radio_buttons === \'radio_option_1\'){ echo \'checked="checked"\'; } ?> />
</label><br>
<label for="<?php echo $this->get_field_id(\'radio_option_2\'); ?>">
<?php _e(\'Option 2:\'.$radio_buttons); ?>
<input class="" id="<?php echo $this->get_field_id(\'radio_option_2\'); ?>" name="<?php echo $this->get_field_id(\'radio_buttons\'); ?>" type="radio" value="radio_option_2" <?php if($radio_option_1 === \'radio_option_2\'){ echo \'checked="checked"\'; } ?> />
</label>
</p>
<?php
}
// 3
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[\'radio_buttons\'] = strip_tags($new_instance[\'radio_buttons\']);
return $instance;
}
// 4
function widget($args, $instance) {
extract( $args );
$radio_buttons = $instance[\'radio_buttons\'];
?>
<?php echo $before_widget; ?>
<p>Radio button value/status: <?php echo $radio_buttons; ?></p>
<?php echo $after_widget; ?>
<?php
}
SO网友:Jonas Lundman
对于任何与javascript问题斗争的人,wrong checked gets checked, 使舒尔HTML不为空checked=""
属性。仅在选定单选按钮上添加属性。
此代码适用于带有单选按钮的小部件without if-statements:
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>\';
}