我有这个下拉列表取决于其他两个下拉列表的选择,
我已经调试了它,我看到的唯一错误是,它永远不会成功
这是我的javascript
jQuery(document).ready(function(){
$("#GRADE").change(function() {
var TRANCHE = {"TRANCHE" : $('#TRANCHE').val()};
var GRADE = {"GRADE" : $('#GRADE').val()};
console.log(TRANCHE);
console.log(GRADE);
$.ajax({
type: "POST",
dataType: "json",
data: {
TRANCHE : "TRANCHE",
GRADE : "GRADE"
},
url: "<?php base_url(); ?>EmployeeSalary/dependent_dropdown2/",
success: function(data){
var select = $('#SAL_ID');
select.html('');
$.each(data, function(i, option){
select.append("<option value='"+option.ID+"'>"+option.AMOUNT+"</option>");
});
}
});
});
});
这是我的控制器
public function dependent_dropdown2()
{
if(isset($_POST['TRANCHE']))
{
$data = $_POST['TRANCHE'];
$data1 = $_POST['GRADE'];
$this->output
->set_content_type("application/json")
->set_output(json_encode($this->EmployeeSalary_Model->getType2($data, $data1)));
}
}
为什么它不能触发成功功能呢?
答案 0 :(得分:1)
可能是控制器没有向浏览器返回任何内容。
不使用output
类直接尝试。
public function dependent_dropdown2()
{
if(isset($_POST['TRANCHE']))
{
$data = $_POST['TRANCHE'];
$data1 = $_POST['GRADE'];
echo json_encode($this->EmployeeSalary_Model->getType2($data, $data1));
}
}
看起来提供给$.ajax
的数据结构也已关闭。
jQuery(document).ready(function () {
$("#GRADE").change(function () {
var posting = {TRANCHE: $('#TRANCHE').val(), GRADE: $('#GRADE').val()};
$.ajax({
type: "POST",
dataType: "json",
data: posting,
url: "<?php base_url(); ?>EmployeeSalary/dependent_dropdown2/",
success: function (data) {
console.log(data);
var select = $('#SAL_ID');
select.html('');
$.each(data, function (i, option) {
select.append("<option value='" + option.ID + "'>" + option.AMOUNT + "</option>");
});
}
});
});
});
答案 1 :(得分:0)
尝试从varibles中删除对象,
jQuery(document).ready(function(){
$("#GRADE").change(function() {
var TRANCHE = $('#TRANCHE').val();
var GRADE = $('#GRADE').val();
console.log(TRANCHE);
console.log(GRADE);
$.ajax({
type: "POST",
dataType: "json",
data: {
TRANCHE : "TRANCHE",
GRADE : "GRADE"
},
url: "<?php base_url(); ?>EmployeeSalary/dependent_dropdown2/",
success: function(data){
var select = $('#SAL_ID');
select.html('');
$.each(data, function(i, option){
select.append("<option value='"+option.ID+"'>"+option.AMOUNT+"</option>");
});
}
});
});
});
----------------------添加了返回码------------------- 强>
返回方法使用
public function dependent_dropdown2_asd()
{
if(isset($_POST['TRANCHE']))
{
$TRANCHE = $_POST['TRANCHE'];
$GRADE = $_POST['GRADE'];
$data=array('Responce'=>200);
header('Content-Type: application/json');
echo json_encode($data);
}
}
答案 2 :(得分:0)
你的ajax电话中有一点错字。 我认为你必须在ajax的数据对象中传递变量(TRANCHE,GRADE)而不是传递字符串(“TRANCHE”,“GRADE”)。 这是代码
jQuery(document).ready(function(){
$("#GRADE").change(function() {
var TRANCHE = {"TRANCHE" : $('#TRANCHE').val()};
var GRADE = {"GRADE" : $('#GRADE').val()};
console.log(TRANCHE);
console.log(GRADE);
$.ajax({
type: "POST",
dataType: "json",
data: {
TRANCHE : TRANCHE, //pass the variable
GRADE : GRADE
},
url: "<?php base_url(); ?>EmployeeSalary/dependent_dropdown2/",
success: function(data){
var select = $('#SAL_ID');
select.html('');
$.each(data, function(i, option){
select.append("<option value='"+option.ID+"'>"+option.AMOUNT+"</option>");
});
}
});
});
});
其他事情看起来不错。您可以通过打印它来调试发布请求,如
public function dependent_dropdown2()
{
if(isset($_POST['TRANCHE']))
{
$data = $_POST['TRANCHE'];
$data1 = $_POST['GRADE'];
print_r($data1);
print_r($data2);
}
}