gpt4 book ai didi

javascript - 我们可以在 javascript 中嵌入 form_dropdown 函数吗?

转载 作者:行者123 更新时间:2023-11-30 22:35:27 25 4
gpt4 key购买 nike

您好,我正在使用 codeigniter。我需要在 javascript 中嵌入一个 form_dropdown。我有一个带有按钮添加行的表。当我点击添加时,我复制了上面的同一行。在我的 View 页面中,我有一个 javascript 来添加动态表行。现在我无法在其中添加下拉菜单。我有一个 Controller 文件,我在其中传递应显示为下拉框的数据。我的 Controller 功能:

class Admin_billing extends CI_Controller {

public function __construct()
{
parent::__construct();

$this->load->model('billing_model');

if(!$this->session->userdata('is_logged_in')){
redirect('admin/login');
}
}

public function ticket($id)
{
$this->load->library('session');
$this->session->unset_userdata('client_detail');
$id=$this->uri->segment(4);

$data['id']=$this->uri->segment(4);

$data['employee']=$this->billing_model->emp_name();

$data['main_content'] = 'admin/billing/ticket_page';
$this->load->view('includes/template', $data);
}
}

我的模型函数:

<?php
class billing_model extends CI_Model {
public function __construct()
{
$this->load->database();

}
public function emp_name()
{

$this->db->select('employee.first_name');
$this->db->from('employee');
$this->db->group_by('employee.id');
$query = $this->db->get();

return $query->result_array();
}


}
?>

这里我有一个 emp_name 函数来从我的数据库中获取员工姓名。我的查看页面:

<script>
function displayResult() {
var row = document.getElementById("test").insertRow(-1);
row.innerHTML = '<td>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[]" 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>
<?php

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

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

这是我在上表中的查看页面 我的第一个字段是下拉 php 代码。这适用于我表中的静态行。当我使用 javascript 添加新行时,我无法获得下拉列表。如何处理这个问题?有人可以帮我编写代码吗?

最佳答案

首先,PHP 是服务器端语言。如果您仅在 javascript 中附加到 html 您的行,则不会执行 php。

为此您需要使用 ajax。 jQuery 解决方案:

  1. 在服务器端 (php),您需要准备 url(函数)以提供新行 chop 数据。
  2. On buton onClick 事件触发 jQuery ajax http://api.jquery.com/jquery.ajax . url 参数指向准备好的 url。
  3. ajax 请求完成后,您将响应数据设置为表格的新行。

$.ajax({
url: "DOMAIN/newRow",
})
.done(function(data) {
$('table#test tbody').append(data);
}
});

PHP(在您的管理计费 Controller 中)

我对cakePHP不是很熟悉,就这么简单。您需要编写返回生成的行 html 的函数。

public function newRow()
{
$options_employee = 'prepare this variable here';
$dropdown = form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');

// Send data to browser (return is only for demonstration)
return '<tr><td>' . $dropdown . '</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></tr>';
}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com