Type -6ren">
gpt4 book ai didi

javascript - 显示存储在下拉菜单中的数据

转载 作者:行者123 更新时间:2023-11-29 23:08:47 25 4
gpt4 key购买 nike

我无法在下拉菜单中显示结果。对于下拉菜单,我没有直接选择与出现的 id 相匹配的类型。

如何让下拉菜单根据item ID出现?

这个 View

label for="type">Type</label>
<select class="form-control" id="type_id" name="type_id">
<?php foreach($type as $types) : ?>
<?php if( $types == $barang['type_id']) : ?>
echo '<option value="<?php echo "$types->type_id"?>" selected><?php echo "$types->type_nama"?></option>';
<?php else : ?>
echo '<option value="<?php echo "$types->type_id"?>"><?php echo "$types->type_nama"?></option>';
<?php endif; ?>
<?php endforeach; ?>
</select>

这个模型

public function getTypeQuery()
{
$this->db->order_by('type_id', 'asc');
$query = $this->db->get('type');
return $query->result();
}

public function getTypeById($id)
{
return $this->db->get_where('barang', ['barang_id' => $id])->row_array();
}

public function ubahTypeBarang ()
{
$data = [
"barang_kode"=> $this->input->post('barang_kode', true),
"type_id"=> $this->input->post('type_id', true),
"barang_nama"=> $this->input->post('barang_nama', true),
];

$this->db->where('barang_id', $this->input->post('barang_id'));
$this->db->update('barang', $data);

}

这个 Controller

public function ubah_type($id)
{
$data['judul'] = 'Form Ubah Data Mahasiswa';
$data['barang'] = $this->Gudang_model->getTypeById($id);
$data['type'] = $this->Gudang_model->getTypeQuery();

$this->form_validation->set_rules('barang_kode', 'Kode', 'required');
$this->form_validation->set_rules('type_id', 'Type', 'required');
$this->form_validation->set_rules('barang_nama', 'Nama Barang', 'required');

if($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('gudang/ubah_type', $data);
$this->load->view('templates/footer');
} else {
$this->Gudang_model->ubahTypeBarang();
$this->session->set_flashdata('flash', 'Diubah');
redirect('gudang/lihat_type');
}

}

`

最佳答案

尝试像这样改变 View :

<label for="type">Type</label>
<select class="form-control" id="type_id" name="type_id">
<?php foreach($type as $types) : ?>
<?php if( $types->type_id == $barang['type_id']) : ?>
<option value="<?php echo $types->type_id ?>" selected><?php echo $types->type_nama ?></option>
<?php else : ?>
<option value="<?php echo $types->type_id ?>"><?php echo $types->type_nama ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>

关于javascript - 显示存储在下拉菜单中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54231108/

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