我需要将样式和只读属性应用于drupal表单的输入元素。
我编写了以下代码:
$form['precio'] = array(
'#type' => 'textfield',
'#title' => t('Precio'),
'#default_value' => ''.$precio,
'#size' => 20,
'#required' => TRUE,
'#attributes' => array($inputAtributo => 1),
'#description' => t('Modifica el precio '),
);
并在
'#attributes' => array($inputAtributo => 1),
在创建表单之前,我检查此输入是否应为只读并应用某些样式:
if ($tarifa !='' & $tarifa=='N')
$inputAtributo=" readonly style=\"background: none repeat scroll 0 0 #EAEAEA;\" ";
else
$inputAtributo = "";
可以,但是我认为编码不正确
html代码显示以下内容:
<input id="edit-precio" class="form-text required" type="text" maxlength="128" size="20" value="258" name="precio" ="1"="" style="background: none repeat scroll 0 0 #EAEAEA;" readonly="">
我该如何做得更好?
最佳答案
#attributes
必须是键值对数组。
所以数组应该看起来像
'#attributes' => array(
'readonly'=>'readonly',
'style'=>'background: none repeat scroll 0 0 #EAEAEA;'
);
关于Drupal 7-输入表单只读并应用样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952001/