8

如何在此代码中使文本框只读

<?php 
    echo form_input(array(
        'name'=>'price',
        'value'=>$item['price'],
        'size'=>'6'
    ));
?>

我只希望某些用户只能读取该值而不能更改它。

4

5 回答 5

15

试试喜欢

<?php 
  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6',
     'readonly'=>'true'));
      //Or 'readonly'=>'readonly'
?>
于 2013-03-20T13:15:17.977 回答
3
$options    =   array(
                    'name'      =>  'price',
                    'value'     =>  $item['price'],
                    'size'      =>  '6'
);

if(!$allowed_user){
    $options['readonly']    =   'readonly'
}

echo form_input($options);
于 2013-03-20T13:17:48.123 回答
3
# Try pure PHP
if(isset($_POST['something'])){
# Do not set the readonly attribute
$readonly ='';
}else{
# Set the readonly attribute
$readonly = 'readonly';
}
<input type="text" <?php echo $readonly; ?>>
于 2015-08-10T07:42:29.660 回答
2

你可以这样做:

<?php 
  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6',
     'readonly'=>'readonly')); 
?>

这将与 xhtml 兼容。

请确保不要在服务器端读取它,不要信任客户端数据,因为可以更改 HTML 以允许修改值。

于 2013-03-20T13:19:39.547 回答
0

有很多方法可以做到这一点:

来自 CI 表单字段:

  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6','readonly'=>'true'));

来自 jquery/javascript:

使用字段的 id 作为:

 $("#id_offield").attr("readonly",true);

在 html 中:

<textarea rows="4" cols="50" readonly>

ETC!

希望它会有所帮助!

于 2013-03-20T13:20:49.053 回答