如何在 selecting select 选项时从数据库中获取数据 jquery
How to fetch the data from database while selecting a select option from jquery
我正在为我的项目使用 codeigniter,我正在传递视图中的数组值。
让我详细介绍一下视图、控制器和模型结构。
型号:
public function getTournament() {
$this->db->select('tourn_id,tourn_name,tourn_teams');
$this->db->from('tournaments');
$query = $this->db->get();
return $query->result_array();
}
控制器
public function index() {
$result['tournament']=$this->matches_model->getTournament();
$this->template->set_layout('admin')->enable_parser(FALSE)->title('Add Matches - Cricnepal Live Update')->build('admin/add/matches_view', $result);
}
查看:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value=''>--- Select Tournament ---</option>
<?php
foreach ($tournament as $row):
$match_TournamentName=$row['tourn_name'];
$match_TournamentID=$row['tourn_id'];
$teamlist=$row['tourn_teams'];
echo '<option value='.$match_TournamentID.'>'.$match_TournamentName.'</option>';
endforeach;
?>
</select>
问题:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value="">--- Select Tournament ---</option>
<option value="1">Cricnepal Cricket Tournament</option>
<option value="2">Nepal Cricket Tournament</option>
</select>
我想在其下方显示一个新的 select 选项字段,该字段显示受尊重的 selected 选项值的数据。
例如,如果我 select "Cricnepal Cricket Tournament" 那么我需要立即使用 jQuery 或其他方法从数据库中获取与之相关的所有关联数据,以便可以添加作为新的选项元素。
您可以使用如下所示的 jquery 'ajax' 调用:
$('#tournament_match').change(function() {
var selected_option = $(this).val();
$.ajax({
url: <YOUR URL TO HANDLE THE REQUEST>+"/"+selected_option,
type: 'post',
cache: false,
success: function(return_data) {
$('#second_select').html(return_data);
}
});
});
其中 'second_select' 是第二个下拉列表的 ID。
这是您的 html
将如何进行:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value="">--- Select Tournament ---</option>
<option value="1">Cricnepal Cricket Tournament</option>
<option value="2">Nepal Cricket Tournament</option>
</select>
<select name="second_select" id="second_select"></select>
你的 js
将是:
$('#tournament_match').change(function() {
var selected_option = $(this).val();
$.ajax({
url: <YOUR URL TO HANDLE THE REQUEST>+"/"+selected_option,
type: 'post',
cache: false,
success: function(return_data) {
$('#second_select').html(return_data);
}
});
});
而处理 ajax 请求的 php
将是这样的:
$newoptions = ['apples','oranges','bananas']; //values from the db or some api source.
$ret_val = '';
foreach($newoptions as $option) {
$ret_val .= "<option>$option</option>";
}
echo $ret_val;
给你伙计。我认为你应该不再有问题了。
我正在为我的项目使用 codeigniter,我正在传递视图中的数组值。
让我详细介绍一下视图、控制器和模型结构。
型号:
public function getTournament() {
$this->db->select('tourn_id,tourn_name,tourn_teams');
$this->db->from('tournaments');
$query = $this->db->get();
return $query->result_array();
}
控制器
public function index() {
$result['tournament']=$this->matches_model->getTournament();
$this->template->set_layout('admin')->enable_parser(FALSE)->title('Add Matches - Cricnepal Live Update')->build('admin/add/matches_view', $result);
}
查看:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value=''>--- Select Tournament ---</option>
<?php
foreach ($tournament as $row):
$match_TournamentName=$row['tourn_name'];
$match_TournamentID=$row['tourn_id'];
$teamlist=$row['tourn_teams'];
echo '<option value='.$match_TournamentID.'>'.$match_TournamentName.'</option>';
endforeach;
?>
</select>
问题:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value="">--- Select Tournament ---</option>
<option value="1">Cricnepal Cricket Tournament</option>
<option value="2">Nepal Cricket Tournament</option>
</select>
我想在其下方显示一个新的 select 选项字段,该字段显示受尊重的 selected 选项值的数据。
例如,如果我 select "Cricnepal Cricket Tournament" 那么我需要立即使用 jQuery 或其他方法从数据库中获取与之相关的所有关联数据,以便可以添加作为新的选项元素。
您可以使用如下所示的 jquery 'ajax' 调用:
$('#tournament_match').change(function() {
var selected_option = $(this).val();
$.ajax({
url: <YOUR URL TO HANDLE THE REQUEST>+"/"+selected_option,
type: 'post',
cache: false,
success: function(return_data) {
$('#second_select').html(return_data);
}
});
});
其中 'second_select' 是第二个下拉列表的 ID。
这是您的 html
将如何进行:
<select name="tournament_match" class="form-control" id="tournament_match">
<option value="">--- Select Tournament ---</option>
<option value="1">Cricnepal Cricket Tournament</option>
<option value="2">Nepal Cricket Tournament</option>
</select>
<select name="second_select" id="second_select"></select>
你的 js
将是:
$('#tournament_match').change(function() {
var selected_option = $(this).val();
$.ajax({
url: <YOUR URL TO HANDLE THE REQUEST>+"/"+selected_option,
type: 'post',
cache: false,
success: function(return_data) {
$('#second_select').html(return_data);
}
});
});
而处理 ajax 请求的 php
将是这样的:
$newoptions = ['apples','oranges','bananas']; //values from the db or some api source.
$ret_val = '';
foreach($newoptions as $option) {
$ret_val .= "<option>$option</option>";
}
echo $ret_val;
给你伙计。我认为你应该不再有问题了。