如何将数据库值添加到我的jquery生成的组合框
<script>
$('#button').on('click', function(){
$(document).ready(function () {
$('#button').on('click', function(){
$('.tablesaw').find('tbody').append($('<tr><td><select id="heard" class="form-control" name = "fieldname"><option value="0">-- Choose --</option><option value = "Amount">Amount</option></select></td><td><input type = "text" name = "condition" class="form-control col-md-7 col-xs-12"></td><td><select id="heard" class="form-control"><option>--Choose--</option></select></td><td><a href="#" class="btn btn-info btn-xs"><i class="fa fa-pencil"></i> Edit </a><a href="#" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i> Delete </a></td></tr>'));
$('.e1').select2();
$('.e2').select2().on('change', function () {
$(this).next()
.find('.select2-selection')
.css({ backgroundColor: this.value });
}).trigger('change');
});
});
答案 0 :(得分:0)
将数据从控制器传递到视图,并在javascript生成的组合框中显示php值
例如:
<强>控制器强>
class Test extends CI_Controller {
public function index()
{
$this->db->where('columnname', 'value');
$query = $this->db->get('tablename');
$comboboxHtml = '<select id="heard" class="form-control" name = "fieldname"><option value="0">-- Choose --</option>';
foreach ($query->result() as $row) {
$comboboxHtml .= '<option value = "' . $row->optionValue . '">' . $row->optionName . '</option>';
}
$comboboxHtml .= '</select>';
$data = array("view" => "test/index",
"comboboxHtml" => $comboboxHtml,
"page_title" => "Test");
$this->load->view('templates/default', $data);
}
}
然后将数据添加到Javascript生成的组合框中,如
<script>
$('#button').on('click', function(){
$(document).ready(function () {
$('#button').on('click', function(){
$('.tablesaw').find('tbody').append($('<tr><td><?php echo $comboboxHtml; ?></td><td><input type = "text" name = "condition" class="form-control col-md-7 col-xs-12"></td><td><select id="heard" class="form-control"><option>--Choose--</option></select></td><td><a href="#" class="btn btn-info btn-xs"><i class="fa fa-pencil"></i> Edit </a><a href="#" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i> Delete </a></td></tr>'));
$('.e1').select2();
$('.e2').select2().on('change', function () {
$(this).next()
.find('.select2-selection')
.css({ backgroundColor: this.value });
}).trigger('change');
});
});
</script>