简体   繁体   English

表单下拉列表的占位符选择类型?

[英]Place holder for form dropdown select type?

how do i add a "Please select an option" to be the default shown option in a drop down select box in a form. 如何在表单的下拉选择框中添加“请选择一个选项”作为默认显示的选项。

I'm modifying a magento module adding in some new input fiends on the frontend that saves it to a table in magento's database. 我正在修改magento模块,在前端添加了一些新的输入恶魔,将其保存到magento数据库中的表中。 the form works correctly saving the information to the database but it always shows the 1st item from the array used to list the options. 该表格可以正确地将信息保存到数据库,但是它始终显示用于列出选项的数组中的第一项。 so if the user doent change the option, then array [0] will be added to the database. 因此,如果用户确实更改了该选项,则数组[0]将添加到数据库中。 i want to add a place holder that wont be added to the database, so it will prompt the user to select an option. 我想添加一个不会添加到数据库的占位符,因此它将提示用户选择一个选项。

this is the information that is called: 这就是所谓的信息:

$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"),
    );

this is what is used to show the input field: 这是用来显示输入字段的内容:

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>

车辆制造

In your above code 在上面的代码中

<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>

add that default option before foreach like this 像这样在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>

You can use any value and name for this. 您可以为此使用任何值和名称。 Now use can use javascript to validate it. 现在使用可以使用javascript对其进行验证。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM