Javascript 更改codeigniter中数据库中另一个选择框的更改事件时选择框的值
我有一个视图文件作为Javascript 更改codeigniter中数据库中另一个选择框的更改事件时选择框的值,javascript,jquery,codeigniter,Javascript,Jquery,Codeigniter,我有一个视图文件作为 <select name="tournaments" id ="tournaments" class="form-control"> <?php foreach ($tournaments as $tournament): ?> <option value="<?php echo $tournament->TournamentID ?>"><?php echo $tournament->TournamentN
<select name="tournaments" id ="tournaments" class="form-control">
<?php foreach ($tournaments as $tournament): ?>
<option value="<?php echo $tournament->TournamentID ?>"><?php echo $tournament->TournamentName ?></option>
<?php endforeach ?>
</select>
控制器文件将返回多行,我需要在ajax的成功事件中捕获这些行,并相应地更新第二个选择框,我该怎么做?您应该看看form\u helper文件,其中有许多用于构建表单输入的有用函数,您所要做的就是加载此helper: 在application/config/autoload.php中,转到helper并添加helper名称,如下所示:
$autoload['helper'] = array('form');
接下来,转到视图文件并更改以下内容:
<select name="tournaments" id ="tournaments" class="form-control">
<?php foreach ($tournaments as $tournament): ?>
<option value="<?php echo $tournament->TournamentID ?>"><?php echo $tournament->TournamentName ?></option>
<?php endforeach ?>
</select>
为此:
<?php
// $tournaments must me an array, use ->result_array() instead of ->result_object()
$tournaments = array_column($tournaments, 'TournamentName', 'TournamentID');
echo form_dropdown('tournaments', $tournaments, '', 'id="tournaments" class="form-control")
?>
对于成功回调函数,可以使用$.each或just For循环提取每个元素并为选择输入创建选项:
$.ajax({
type:"POST",
dataType: 'json',
url:"<?php echo base_url('admin/dashboard/update_match_box') ?>",
data: {tournament:data},
success: function(data) {
$('select#secondSelect').html('');
$.each(data, function(item) {
$("<option />").val(item.id)
.text(item.name)
.appendTo($('select#secondSelect'));
});
}
});
<?php
// $tournaments must me an array, use ->result_array() instead of ->result_object()
$tournaments = array_column($tournaments, 'TournamentName', 'TournamentID');
echo form_dropdown('tournaments', $tournaments, '', 'id="tournaments" class="form-control")
?>
$.ajax({
type:"POST",
dataType: 'json',
url:"<?php echo base_url('admin/dashboard/update_match_box') ?>",
data: {tournament:data},
success: function(data) {
$('select#secondSelect').html('');
$.each(data, function(item) {
$("<option />").val(item.id)
.text(item.name)
.appendTo($('select#secondSelect'));
});
}
});