2010-05-18 78 views
5

我有这组单选按钮,其中每个单独按钮都具有通过样式属性设置的自己的位置。我想如何使用drupal form api来归档相同的文件。我发现整体风格如何,但不是个体内部的控制。下面是我的HTML代码的样子 -以drupal的形式设计各个单选按钮

<input type="radio" name="base_location" checked="checked" value="0" style="margin-left:70px;float:left;"/><span style="float:left;">District</span> 
    <input type="radio" name="base_location" value="1" style="margin-left:50px;float:left;"/><span style="float:left;">MRT</span> 
    <input type="radio" name="base_location" value="2" style="margin-left:60px;float:left;"/><span style="float:left;">Address</span> 

这是Drupal的代码,我被堵在 -

$form['base_location'] = array(
    '#type' => 'radios', 
    '#title' => t('base location'), 
    '#default_value' => variable_get('search_type', 0), 
    '#options' => array(
'0'=>t('District'), 
'1'=>t('MRT'), 
'2'=>t('Address')), 
    '#description' => t('base location'), 

我知道#类型=>无线电的存在。但是,在这方面,我不知道如何将所有单选按钮组合在一起。如果我为它们全部使用相同的数组键,它们会相互碰撞。如果我不这样做,他们不会被视为同一组的一部分。我提前谢谢你。

回答

2

如果你使用Drupal的6.x的形式API和#type=>radioshttp://api.drupal.org/api/function/theme_radios/6)每个无线电元件将有其独特的ID,你可以用它来申请适当的CSS。

比如你提供

$form['base_location'] = array(
    '#type' => 'radios', 
    '#title' => t('base location'), 
    '#default_value' => variable_get('search_type', 0), 
    '#options' => array(
    '0'=>t('District'), 
    '1'=>t('MRT'), 
    '2'=>t('Address')), 
    '#description' => t('base location'), 
); 

应该输出标记像这样:

<div id="base-location-0-wrapper" class="form-item"> 
    <label for="base-location-0" class="option"><input type="radio" class="form-radio" value="0" name="base_location" id="base-location-0"> District</label> 
</div> 
<div id="base-location-1-wrapper" class="form-item"> 
    <label for="base-location-1" class="option"><input type="radio" class="form-radio" value="1" name="base_location" id="base-location-1"> MRT</label> 
</div> 
<div id="base-location-2-wrapper" class="form-item"> 
    <label for="base-location-2" class="option"><input type="radio" class="form-radio" value="2" name="base_location" id="base-location-2"> Address</label> 
</div> 

应用下面的CSS,你应该设置。

#base-location-0-wrapper, 
    #base-location-1-wrapper, 
    #base-location-2-wrapper { 
    display:inline; 
    margin-left:50px; 
    } 
+0

感谢您的解决方案。 – Andrew 2010-05-19 03:33:48

+0

除环境之外的ID有时不可靠。例如,如果收音机是根据分类术语生成的,则可以获得不能可靠地使用的 -tid- 。 – 2012-11-05 12:19:31

1

答案是将CSS与您已有的html结合使用。它看起来像你只是想改变单选按钮的显示为'内联'10px的边距,你可以做。 (如果你没有打破无线电成单独的元素一个字段内,他们会不再互相交流。)

+0

我想我被标记类型卡住,并直接写入相应的html代码。但我不确定drupal是否仍将它们视为控件。会吗? – Andrew 2010-05-18 14:06:26

+0

你为什么坚持'标记类型'?使用Drupal输出表单并使用CSS进行设置。问题在哪里? – lazysoundsystem 2010-05-18 21:59:46

+0

问题是,我不知道drupal自己为每个元素生成id,并且,我可以想到的唯一方法是将其标记为我想要的结果,按原样输出标记,并为每个元素分配class或set样式。但是,现在很明显;我需要的是 - 用drupal生成的id定义css风格。不管怎么说,还是要谢谢你。 – Andrew 2010-05-19 03:38:43