gpt4 book ai didi

javascript - 使用 codeigniter 选择的显示选项

转载 作者:行者123 更新时间:2023-12-03 02:53:42 25 4
gpt4 key购买 nike

我想询问在页面寄存器中使用 2 个选项卡选择的代码选项

table prodi
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id_prodi | varchar(3) | NO | PRI | NULL | |
| nama_prodi | varchar(30) | NO | | NULL | |
| id_fakultas | varchar(3) | NO | MUL | NULL | |
| id | int(2) | NO | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+

table jurusan
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| id_fakultas | varchar(3) | NO | PRI | NULL | |
| nama_fakultas | varchar(40) | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+

这里的 View :

 <label for="fakultas">Fakultas : </label>
<select id="fakultas" name="fakultas_user" onChange="fakultas">

<?php
foreach ($qfakultas as $fakultas) {
echo "<option value=".$fakultas->kd_fakultas.">".$fakultas->nama_fakultas."</option>";
}
?>
</select>

<hr>

<label for="jurusan">Jurusan : </label>
<select id="jurusan" name="jurusan_user" onChange="jurusan">
<?php
foreach ($qprodi as $prodi) {
echo "<option value=".$prodi->kd_prodi.">".$prodi->nama_prodi."</option>";
}
?>
</select>

但我会根据“id_fakultas”显示“nama_prodi”

register with option value

当我点击'nama_fakultas'然后根据'id_fakultas'显示'nama_prodi'时如何显示?

谢谢

最佳答案

这里是我可以帮忙的Ajax代码,只得到nama_prodi基于id_falkutas .

You can use this code for your project, there is something you need to do.
Not just copy the code. Just a few step
(ID Lang : Jangan dicopy mentah2, hanya beberapa langkah saja untuk edit).

  1. Change YOUR_URL_HERE into your base_url() or create var for it.
  2. Change nama_controller into your controller name in CI_Controller
  3. Change nama_fungsi into your function to get the data from your model (database).
    Example : ambil_data_jurusan.
  4. Remove your foreach - echo <option> and onchange from your view.
  5. Add the script into your view files or create a new .js file.
  6. Please help me to mark as correct answer if this code helped you. ( tolong dibantu gan )

参见代码示例:

<script>
$("#falkutas").on('change', function() {
$.ajax({
type:"POST",
url:'YOUR_URL_HERE/nama_controller/nama_fungsi',
data:{"falkutas_user":falkutas_user,"csrf_token":$("input[name=csrf_token]").val()},
dataType:'json',
success: function(result) {
for(var i=0; i<result.length; i++) {
res += '<option value="'+result[i]['id_prodi']+'">'+result[i]['nama_prodi']+'</option>';
}
$(document).ready(function() {
$("#prodi").html(res);
});
},
error: function(result) {
$(document).ready(function() {
$("#prodi").html(''); //empty select value
});
}
});
}
</script>

然后,在您的函数代码中:

// Inside function ambil_data_jurusan(), harusnya sudah jalan kalau gini
$return = $this->model->get_jurusan($this->input->post('falkutas',TRUE));
if(count($return)>0) {
echo json_encode($return);
}

在你看来,就这样让selectempty就可以了。

<select id="jurusan" name="jurusan_user">

</select>

关于javascript - 使用 codeigniter 选择的显示选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47727579/

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