在编辑详细信息的表单中,我有一个带有0和1值的单选按钮。我想要的是,在加载表单时,当用户单击edit链接时,单选按钮应该设置为数据库中的值。一旦用户更改了它并提交了表单,它应该保留提交的值。这怎么可能?
我试过使用下面的代码,但它不起作用。我知道这样做是不可能的。$row->videotype具有数据库中的值。
<input type="radio" name="trailer" value="1" <?php 
    if(set_radio('trailer', '1', TRUE)) {
        echo set_radio('trailer', '1', TRUE); 
    } else { 
        if($row->videotype==1) ?> checked=checked <?php 
    } ?> />Yes
<input type="radio" name="trailer" value="0" <?php 
    if(set_radio('trailer', '0')) {
        echo set_radio('trailer', '0');  
    } else {
        if($row->videotype==0) ?> checked=checked <?php 
    } ?> />No在表单提交函数(在控制器中)中,我将此设置为表单验证:
$this->form_validation->set_rules('trailer', 'Trailer', 'is_numeric');  有人能帮我解决这个问题吗?提前谢谢。
发布于 2014-04-01 11:14:11
set_value函数在编辑表单验证中很有用,请尝试这样做。
<input type="radio" name="trailer" value="1" <?php 
    echo set_value('trailer', $row->videotype) == 1 ? "checked" : ""; 
?> />Yes
<input type="radio" name="trailer" value="0" <?php 
    echo set_value('trailer', $row->videotype) == 0 ? "checked" : ""; 
?> />No在控制器中,验证规则很好,如果post数据有空白,也请使用trim规则。
$this->form_validation->set_rules('trailer', 'Trailer', 'trim|is_numeric');  编辑:set_radio函数在编辑模式下创建问题表单验证请使用set_value函数
发布于 2014-04-01 11:20:36
set_radio是一个文档不全的CI函数。你用得不对。你最好不要用它。这一点更简单:
<input type="radio" name="trailer" value="1" <?php if($row->videotype==1) ?> checked=checked <?php } ?> />Yes
<input type="radio" name="trailer" value="0" <?php if($row->videotype==0) ?> checked=checked <?php } ?> />No发布于 2016-08-19 09:40:31
使用代码点火器形式函数:
<?php $selected = 'Yes'; ?>
<?php echo form_label('Trailer: ', 'trailer'); ?>
<?php echo form_label('Yes', 'trailer_yes') . form_radio(array('name' => 'trailer', 'value' => 'Yes', 'checked' => ('Yes' == $selected), 'id' => 'trailer_yes')); ?>  
<?php echo form_label('No', 'trailer_no') . form_radio(array('name' => 'trailer', 'value' => 'No', 'checked' => ('No' == $selected), 'id' => 'trailer_no')); ?>
<?php echo form_error('trailer'); ?>https://stackoverflow.com/questions/22783962
复制相似问题