简体   繁体   English

如何使用JavaScript生成输入类型下拉列表?

[英]How to generate a input type dropdown using javascript?

Hi I am using codeigniter. 嗨,我正在使用codeigniter。 I am adding row dynamically using javascript when button is clicked. 单击按钮时,我正在使用javascript动态添加行。 Here is my script 这是我的剧本

<script>
function displayResult() {
    var row = document.getElementById("test").insertRow(-1);
    row.innerHTML = '<td><input type="text" name="employee[]" value="" style="width:80px;"/></td><td><input type="text" name="start_time[]" value="" style="width:35px;"/></td><td><input type="text" name="pid[]" style="width:35px;"/></td><td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td><td><input type="text" class="type" value="" style="width:45px;"/></td><td><input type="text" class="qty_prch" value="" style="width:45px;"/></td><td><input type="text" class="qty_used" value="" style="width:45px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td>';
}
</script> 

Here in this script I am generating a row with many textbox. 在此脚本中,我正在生成包含许多文本框的行。 Now I need to have a form_dropdown instead of the first text box in the script( employee[] ) My form_dropdown code: Here I get the data from the controller file. 现在,我需要一个form_dropdown而不是脚本中的第一个文本框( employee[] )我的form_dropdown代码:在这里,我从控制器文件中获取数据。 I have this employee detail in my database 我的数据库中有此员工详细信息

<?php
      //form data
      $attributes = array('class' => 'form-horizontal', 'id' => '');
      $options_employee = array('' => "Select");
      foreach ($employee as $row)
      {
        $options_employee[$row['name']] = $row['name'];
      }

      ?>

This is how I use it in view page. 这就是我在查看页面中使用它的方式。

<?php
          echo '<div class="control-group">';

            echo '<div class="controls">';

              echo form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');

            echo '</div>';
          echo '</div">';

          ?>

How to do this can someone help me code? 有人可以如何帮助我编码? Edit 01: 编辑01: 在此处输入图片说明

Dropdowns are really easy in JS when you have the data array. 当您拥有数据数组时,下拉菜单在JS中非常容易。

You can use the JS native map function with arrays to easily render menus like the following. 您可以将JS本机地图函数与数组一起使用,以轻松呈现菜单,如下所示。

JSON/Object: JSON /对象:

$options_employee: [
  'Michael',
  'John',
  'Sam'
]

HTML : HTML:

<div id="employee-menu">
  <div class="control-group">
    <div class="controls">
      <select name="employee" class="span2">
        <option></option>
      </select>
    </div>
  </div>
</div>

JS: JS:

mag.module("employee-menu", {
  view: function(state, props) {
    state.option = props.$options_employee.map(function(employee, key) {
      return {
        _value: key,
        _text: employee
      }
    })
  }
}, props)

Here is the full working example: http://jsbin.com/pexifodari/edit?html,js,output 这是完整的工作示例: http : //jsbin.com/pexifodari/edit?html,js,output

Hope that helps! 希望有帮助!

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

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