HTML中包含相同内容的下拉列表

时间:2014-03-14 11:08:35

标签: html drop-down-menu repeat


你好,社区。是否可以在一个位置指定下拉列表结构并将其用于多个下拉列表?例如,我需要5个下拉列表,所有下拉列表都包含3个条目:“基本”,“高级”,“专业”。我真的需要写下5次吗?

<select id="myid" name="myname" size="1"> 
    <option value="1">Basic</option>
    <option value="2">Advanced</option>
    <option value="3">Professional</option>
</select>



BR
Ewgenij

2 个答案:

答案 0 :(得分:2)

我最近做了类似的事情。

很简单。查看下面的代码,我曾经自动输入选择下拉列表的日期:

<select id="dateDropdown"></select> /*give an id for select*/

<script type="text/javascript">
var dateSelectId = document.getElementById("dateDropdown"); /*get the select element by its id*/

for (var i = 0; i < 8 ; i++) { /*for loop to create the options - in my case 8 options */
     var currentDate = moment().subtract('d',i).format("MMM D"); /*ignore this - it basically just gives different dates for me*/

     var paramObj = { /*declaring an obj for the 'option' tag's parameters*/
         'optValue':i, /*value for the 'option' tag*/
         'optText':currentDate /*text for 'option' tag*/
     };

     optionGenerator(dateSelectId,paramObj); /*function which actually creates <option> tags and assigns the parameters to it*/
}; 

下面是我导入的javascript文件,其中包含 optionGenerator()函数

/*Function to dynamically create select list and adding options to it*/

var optionGenerator = function(selectId,paramObj){

    var optionInstance = document.createElement("option"); //creates child <option> element
    optionInstance.value = paramObj.optValue;
    optionInstance.text = paramObj.optText;
    selectId.options.add(optionInstance); //adds the <option> tag with desired values
};

如果你理解,请告诉我。

答案 1 :(得分:1)

使用php,你可以这样做

$select_box = "<select id='myid' name='myname' size='1'> 
                <option value='1'>Basic</option>
                <option value='2'>Advanced</option>
                <option value='3'>Professional</option>
            </select>";

echo $select_box ;// where-ever you want in between your HTML tags

&GT;

例如

<form >
<?php  echo $select_box ; ?>
...other inputs...
</form>
相关问题