简体   繁体   English

形式:从数据库的选择选项值列表中添加新项目

[英]Forms : add new item in select option value list from database

I have a form with a list populated by items taken from a table of database : 我有一个带有列表的表单,该列表由从数据库表中获取的项目填充:

<div class="control-group">
  <label class="control-label" for="selectbasic-0">Problem</label>
  <div class="controls">
    <select id="type" name="type" class="input-xlarge">
        <option value="defaut"></option> 
         <?php
         while($data = mysql_fetch_array($result)) 
        { 
        echo utf8_encode ("<option>".$data[type]."</option>"); 
        }
        ?>
    </select>
  </div>
</div>

That works well, but I would like to add the option for the user to add an item to the select component if their desired response isn't there by default. 效果很好,但是我想为用户添加一个选项,以便在默认情况下不存在其所需响应的情况下,将其添加到选择组件中。 If they add a select option it would be saved to the table so it would show up in future page loads. 如果他们添加选择选项,它将被保存到表中,以便在以后的页面加载中显示。 How could I do this? 我该怎么办? Thank you for the help! 感谢您的帮助!

Just a little javascript will do: 只需一点JavaScript就可以:

var select = document.getElementById('#type');
option = //Get your data you want to add and put it between two <option> tags
select.appendChild(option);

If you want that on a button click, just put the code above inside a function, and have the button call that function on the click. 如果您希望单击按钮,只需将上面的代码放在一个函数中,然后单击按钮就可以调用该函数。

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

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