如何在 cakephp 中通过 jquery ajax 填充 select 列表
How to populate the select list by jquery ajax in cakephp
我正在使用 cakephp,我想获取 selected 学位 ID,并希望在另一个下拉列表中显示属于 selected 学位的科目。我的学历select列表如下
<div class="form-group">
<?php echo $this->Form->input('PrimaryRegister.degree', array(
'options'=>$degrees,
'empty'=>'-- select one --',
'label' => false,
'class' => 'form-control',
'id' => 'degree',
'required'=>'required')
); ?>
</div>
我的jquery在下面
$('#degree').change(function(){
var degree_id=$(this).val();
var base_url='<?php echo $this->webroot; ?>';
$.ajax({
type:'post',
data:'degree_id='+degree_id,
dataType:'json',
url:base_url+'/pages/bla',
async:false,
success: function(data){
$(data).each(function() {
$('.test').append($("<option>").attr('value',this.val).text(this.text));
});
}
})
});
我要显示的结果如下
<div class="form-group">
<label>Main/Core<sup class="madadatory">*</sup></label>
<?php
echo $this->Form->input('PrimaryRegister.MainSubject1',array(
'label'=>false,
'options'=>'',
'empty'=>'-- select one --',
'class'=>'form-control test',
'id'=>'MainSubject1',
'required'=>'required'
)
);
?>
</div>
注意:我得到的数组是 ajax 结果,低于
<pre>Array
(
[1] => Zoology
[2] => Botany
[3] => Plant Science
[4] => Home Science
[5] => Forestry
[6] => Microbiology
[7] => Chemistry
[8] => Polymer Chemistry
[9] => Biochemistry
[10] => Biotechnology
[11] => Physics
[12] => Psychology
[13] => Geology
[14] => Mathematics
[15] => Computer Science
[16] => Electronics
[17] => Geography
[18] => Statistics
[19] => Bioinformatics
[20] => Electronics & Communication
[21] => Acqua Culture and Fishery Microbiology?
[22] => Applied Statistics
[23] => Applied Physics
[24] => Chemistry
[25] => Botany
[26] => Costume and Fashion Technology
[27] => Counselling Psychology
[28] => Genetics
[29] => Environmental Science and Water Management
[30] => Family and Community Science
[31] => Food Technology
)
主题 selecting 字段仅由 jquery 生成,请帮助我
您可以在操作中获取它们后在视图中生成 html string
并在操作的视图上打印字符串。
public function your_function() {
// fetch the data
// set the array for view
}
在视图中
foreach($your_array as $key => $value) {
echo "<option value='$key'>$value</option>;
}
ajax 将得到此 html 作为响应。然后设置 html -
$('.your_drop_down_menu').html(response);
通常当您想要填充 select
标签时,您会用值和文本引用整个标签。
$(data).each(function () {
$("<option value='" + yourValue + "'>" + yourText + "</option>").appendTo('.test');
});
我正在使用 cakephp,我想获取 selected 学位 ID,并希望在另一个下拉列表中显示属于 selected 学位的科目。我的学历select列表如下
<div class="form-group">
<?php echo $this->Form->input('PrimaryRegister.degree', array(
'options'=>$degrees,
'empty'=>'-- select one --',
'label' => false,
'class' => 'form-control',
'id' => 'degree',
'required'=>'required')
); ?>
</div>
我的jquery在下面
$('#degree').change(function(){
var degree_id=$(this).val();
var base_url='<?php echo $this->webroot; ?>';
$.ajax({
type:'post',
data:'degree_id='+degree_id,
dataType:'json',
url:base_url+'/pages/bla',
async:false,
success: function(data){
$(data).each(function() {
$('.test').append($("<option>").attr('value',this.val).text(this.text));
});
}
})
});
我要显示的结果如下
<div class="form-group">
<label>Main/Core<sup class="madadatory">*</sup></label>
<?php
echo $this->Form->input('PrimaryRegister.MainSubject1',array(
'label'=>false,
'options'=>'',
'empty'=>'-- select one --',
'class'=>'form-control test',
'id'=>'MainSubject1',
'required'=>'required'
)
);
?>
</div>
注意:我得到的数组是 ajax 结果,低于
<pre>Array
(
[1] => Zoology
[2] => Botany
[3] => Plant Science
[4] => Home Science
[5] => Forestry
[6] => Microbiology
[7] => Chemistry
[8] => Polymer Chemistry
[9] => Biochemistry
[10] => Biotechnology
[11] => Physics
[12] => Psychology
[13] => Geology
[14] => Mathematics
[15] => Computer Science
[16] => Electronics
[17] => Geography
[18] => Statistics
[19] => Bioinformatics
[20] => Electronics & Communication
[21] => Acqua Culture and Fishery Microbiology?
[22] => Applied Statistics
[23] => Applied Physics
[24] => Chemistry
[25] => Botany
[26] => Costume and Fashion Technology
[27] => Counselling Psychology
[28] => Genetics
[29] => Environmental Science and Water Management
[30] => Family and Community Science
[31] => Food Technology
)
主题 selecting 字段仅由 jquery 生成,请帮助我
您可以在操作中获取它们后在视图中生成 html string
并在操作的视图上打印字符串。
public function your_function() {
// fetch the data
// set the array for view
}
在视图中
foreach($your_array as $key => $value) {
echo "<option value='$key'>$value</option>;
}
ajax 将得到此 html 作为响应。然后设置 html -
$('.your_drop_down_menu').html(response);
通常当您想要填充 select
标签时,您会用值和文本引用整个标签。
$(data).each(function () {
$("<option value='" + yourValue + "'>" + yourText + "</option>").appendTo('.test');
});