Dan*_*iaz 9 drupal drupal-7 drupal-fapi
我需要将样式和只读属性应用于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 '),
);
Run Code Online (Sandbox Code Playgroud)
并在 '#attributes' => array($inputAtributo => 1),
在创建表单之前,我检查这个输入是否应该是readonly并应用一些样式:
if ($tarifa !='' & $tarifa=='N')
$inputAtributo=" readonly style=\"background: none repeat scroll 0 0 #EAEAEA;\" ";
else
$inputAtributo = "";
Run Code Online (Sandbox Code Playgroud)
这有效,但我认为它编码不正确
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="">
Run Code Online (Sandbox Code Playgroud)
我怎么能做得更好?
小智 14
#attributes 必须是一组键值对.
所以数组看起来应该是这样的
'#attributes' => array(
'readonly'=>'readonly',
'style'=>'background: none repeat scroll 0 0 #EAEAEA;'
);
Run Code Online (Sandbox Code Playgroud)
#attributes不打算与风格一起使用.您必须提供一个包含键和值的数组,以再现html属性.class和css比直接在html中添加样式更好.
'#attributes' = array(
'class' => array('readonly-input'),
'readonly' => 'readonly',
)
Run Code Online (Sandbox Code Playgroud)
如果要将其添加到if中,可以执行以下操作:
if ($tarifa !='' & $tarifa=='N') {
$form['precio']['#attributes']['class'][] = 'readonly-input';
$form['precio']['#attributes']['readonly'] = 'readonly';
}
Run Code Online (Sandbox Code Playgroud)
请注意,readonly属性也"readonly"为值,因此它符合xhtml.
现在在样式表中添加一条css规则:
.readonly-input { background: none repeat scroll 0 0 #EAEAEA; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8690 次 |
| 最近记录: |