Codeigniter 中的 ajax jQuery 在下拉菜单更改时如何获取 2 个或更多输入值
$('document').ready(function () {
$('.js-example-basic-single').select2();
$('#empName').on('change', function() {
var quan = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxReq"); ?>',
type: 'post',
data: {quan: quan},
})
.done(function(data) { //<---- we use .done here
$('#empId').val(data);
})
});
$('#empName').on('change', function() {
var dept = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxDept"); ?>',
type: 'post',
data: {dept: dept},
})
.done(function(data) {
$('#department').val(data);
})
});
});
控制器部分
public function ajaxReq() {
$quan = $this->input->post( 'quan' );
$value = $this->salary_model->getVal($quan);
echo $value['empId'];
}
public function ajaxDept() {
$dept = $this->input->post( 'dept' );
$value = $this->salary_model->getDept($dept);
echo $value['department'];
}
型号部分
public function getVal($quan){
$this->db->select('*');
$this->db->from('employee');
$where = array('empId' => $quan );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
public function getDept($dept){
$this->db->select('*');
$this->db->from('employee');
$where = array('department' => $dept );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
错误
遇到 PHP 错误
严重性:警告
消息:尝试访问 null 类型值的数组偏移量
文件名:controllers/Emp_salary.php
行号:133
我该如何解决此问题?
我希望当下拉列表更改时,我会自动获得另一个输入值。
$('document').ready(function () {
$('.js-example-basic-single').select2();
$('#empName').on('change', function() {
var quan = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxReq"); ?>',
type: 'post',
data: {quan: quan},
})
.done(function(data) { //<---- we use .done here
$('#empId').val(data);
})
});
$('#empName').on('change', function() {
var dept = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxDept"); ?>',
type: 'post',
data: {dept: dept},
})
.done(function(data) {
$('#department').val(data);
})
});
});
controller part
public function ajaxReq() {
$quan = $this->input->post( 'quan' );
$value = $this->salary_model->getVal($quan);
echo $value['empId'];
}
public function ajaxDept() {
$dept = $this->input->post( 'dept' );
$value = $this->salary_model->getDept($dept);
echo $value['department'];
}
model part
public function getVal($quan){
$this->db->select('*');
$this->db->from('employee');
$where = array('empId' => $quan );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
public function getDept($dept){
$this->db->select('*');
$this->db->from('employee');
$where = array('department' => $dept );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
Error
A PHP Error was encountered
Severity: Warning
Message: Trying to access array offset on value of type null
Filename: controllers/Emp_salary.php
Line Number: 133
how I can solve this?
I expect that when the dropdown changes I get another input value automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的问题,我理解的是您想要的,当选择下拉列表更改时,Codeigniter 中的 jQuery ajax 自动获取 2 个或更多输入值。
HTML 部分:-
JQuery / Ajax 部分:-
控制器部分:-
模型部分:-
According to Your Question , what i Understand is you want, when the Select dropdown changes get 2 or more input value automatically by jQuery ajax in Codeigniter.
HTML Part :-
JQuery / Ajax Part:-
controller part:-
Model part :-