如何在表单的下拉选择框中添加“请选择一个选项”作为默认显示选项。
我正在修改magento模块,在前端添加一些新的输入恶魔,将其保存到magento数据库中的表中。表单正确地将信息保存到数据库,但它始终显示用于列出选项的数组中的第1项。因此,如果用户改变了该选项,则数组[0]将被添加到数据库中。我想添加一个不会添加到数据库的占位符,因此它会提示用户选择一个选项。
这是所谓的信息:
$fields[] = array(
'name' => 'vehicle_make',
'label' => Mage::helper('sublogin')->__('Make'),
'required' => true,
'type' => 'select',
'style' => 'width:100px',
'cssclass' => '',
'options' => array ("Acura", "Alfa Romeo","Audi", "BMW", "Buickm", "Cadillac", "Chevrolet", "Chrysler", "Dodge", "FIAT", "Ford", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep", "Kia", "Land Rover", "Lexus", "Lincoln", "Maserati", "Mazda", "Mercedes-Benz", "MINI", "Mitsubishi", "Nissan", "Porsche", "Ram", "Scion", "Smart", "Subaru", "Suzuki", "Toyota", "Volkswagen", "Volvo", "Yamaha"),
);
这是用于显示输入字段的内容:
if ($formField['type'] == 'select' || $formField['type'] == 'multiselect')
{
$selectedOptions = $sublogin->getData($formField['name']);
$selectedOptions = explode(',', $selectedOptions);
?>
<select
<?php echo ($formField['type'] == 'multiselect')?"multiple=multiple":""; ?>
id="<?php echo $formField['name'] ?>"
name="<?php echo ($formField['type'] == 'multiselect')? $formField['name'].'[]':$formField['name'] ?>">
<?php foreach ($formField['options'] as $optionValue => $optionLabel) {
$selected = '';
if (in_array($optionValue, $selectedOptions))
$selected = 'selected';
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
} ?>
</select>
答案 0 :(得分:0)
在上面的代码中
<select
<?php echo ($formField['type'] == 'multiselect')?"multiple=multiple":""; ?>
id="<?php echo $formField['name'] ?>"
name="<?php echo ($formField['type'] == 'multiselect')? $formField['name'].'[]':$formField['name'] ?>">
<?php foreach ($formField['options'] as $optionValue => $optionLabel) {
$selected = '';
if (in_array($optionValue, $selectedOptions))
$selected = 'selected';
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
} ?>
</select>
在foreach之前添加默认选项
<select
<?php echo ($formField['type'] == 'multiselect')?"multiple=multiple":""; ?>
id="<?php echo $formField['name'] ?>"
name="<?php echo ($formField['type'] == 'multiselect')? $formField['name'].'[]':$formField['name'] ?>">
//added this line of code
<option name="please-select" value="100">Please select an option</option>
<?php foreach ($formField['options'] as $optionValue => $optionLabel) {
$selected = '';
if (in_array($optionValue, $selectedOptions))
$selected = 'selected';
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
} ?>
</select>
您可以为此使用任何值和名称。现在使用可以使用javascript来验证它。
希望这有帮助。