javascript - 我可以在 javascript 中使用 form_dropdown 吗?

标签 javascript jquery codeigniter

您好,我正在使用 codeigniter 框架。当我单击一个按钮时,我有一个 javascript 来动态创建一个表行。 我需要在动态添加的表格单元格内有一个下拉菜单。这是我到目前为止尝试过的代码。

function displayResult() {
<?php

      $attributes = array('class' => 'form-horizontal', 'id' => '');
      $options_employee = array('' => "Select");
      foreach ($employee as $row)
      {
        $options_employee[$row['first_name']] = $row['first_name'];
      }

      ?>
     var something='<?php echo form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');?>';
     alert(something);

    var row = document.getElementById("test").insertRow(-1);
    row.innerHTML = '<td><div>'+something+'</div></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>';
}

单击按钮时,我调用此 displayResult()。我的控制台出现 2 个错误

1.Uncaught SyntaxError: Unexpected token ILLEGAL
2.Uncaught ReferenceError: displayResult is not defined

有人可以帮助我吗?请帮我修复这段代码。

function displayResult() { <? php

  $attributes = array('class' => 'form-horizontal', 'id' => '');
  $options_employee = array('' => "Select");
  foreach($employee as $row) {
    $options_employee[$row['first_name']] = $row['first_name'];
  }

  ?>
  var something = '<?php echo form_dropdown('
  employee ', $options_employee, set_value('
  employee[]
  '), '
  class = "span2"
  ');?>';
  alert(something);

  var row = document.getElementById("test").insertRow(-1);
  row.innerHTML = '<td><div>' + something + '</div></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>';
}
<div id="form">
  <!-- div form starts here.its for add table  -->
  <table id="test">
    <thead>
      <tr>
        <td style="width:80px;">
          employee
        </td>
        <td style="width:35px;">
          start time
        </td>
        <td style="width:35px;">
          id
        </td>
        <td style="width:145px;">
          Description
        </td>
        <td style="width:45px;">
          Type
        </td>
        <td style="width:45px;">
          qty prch
        </td>
        <td style="width:45px;">
          qty used
        </td>
        <td style="width:70px;">
          Price
        </td>
        <td style="width:70px;">
          discount
        <td style="width:70px;">
          Tax
        </td>
        <td style="width:70px;">
          Total
        </td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <?php echo form_dropdown( 'employee', $options_employee, set_value( 'employee[]'), 'class="span2"');?>
        </td>
        <td>
          <input type="text" name="start_time[]" value="" style="width:35px;" />
        </td>
        <td>
          <input type="text" name="pid[]" value="" style="width:35px;" />
        </td>
        <td>
          <input type="text" name="description[]" class="description" value="" style="width:145px;" />
        </td>
        <td>
          <input type="text" name="type[]" class="type" style="width:45px;" />
        </td>
        <td>
          <input type="text" name="qty_prch[]" class="qty_prch" style="width:45px;" />
        </td>
        <td>
          <input type="text" name="qty_used[]" class="qty_used" style="width:45px;" />
        </td>
        <td>
          <input type="text" name="price[]" class="price" style="width:70px;" />
        </td>
        <td>
          <input type="text" name="discount[]" class="discount" style="width:70px;" />
        </td>
        <td>
          <input type="text" name="tax[]" class="tax" style="width:70px;" />
        </td>
        <td>
          <input type="text" name="total[]" class="total" style="width:70px;" />
        </td>

      </tr>
    </tbody>



  </table>

  <div id="add_row">
    <button onClick="displayResult()" class="add_r"></button>
  </div>

最佳答案

这对我来说很好用。谢谢大家的支持!!

function displayResult() {
<?php

      $attributes = array('class' => 'form-horizontal', 'id' => '');
      $options_employee = array('' => "Select");
      foreach ($employee as $row)
      {
        $options_employee[$row['first_name']] = $row['first_name'];
      }
    $dropdown = form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');

      ?>

    var complex = <?php echo json_encode($dropdown); ?>;

    var row = document.getElementById("test").insertRow(-1);
    row.innerHTML = 
        '<td><div>'+complex+'</div></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>';
}

关于javascript - 我可以在 javascript 中使用 form_dropdown 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687376/

相关文章:

php - 返回结果时如何在 AJAX div 中重新加载 MySQL 记录?

javascript - MomentJS 问题处理 2 月 29 日不是闰年

javascript - 为什么谷歌地图无法在 JavaScript 中加载?

javascript - 获取 jQuery 中选定复选框的值

jquery - 显示没有模态框的 Bootstrap 3 灰色背景

codeigniter - 将 Twitter Bootstrap 添加到 CodeIgniter?

php - 无法显示从 AES_DECRYPT 检索到的特殊字符

javascript - 我可以在一个字段中添加带有日期的文本字符串吗?

codeigniter - chrome codeigniter redis 清除 session 问题

javascript - 在 React 中显示三个数组中的元素