layui中的ajax与tp5框架的二级联动

layui中ajax的二级联动,中间也出现了一些特殊情况的问题,就是假如一级下拉框不是数组,只有一个元素,后台加了判断,同理,前台也是,计算传入数据的长度用length,后台返回的是数组
前台代码:
//lay-filter=”hospital” 的值。 select()里的参数就是这个 lay-filter是放在select里的一个属性
form.on(‘select(hospital)’, function(data) {
hospital = data.value;
  hospitalName = data.elem[data.elem.selectedIndex].text;
// alert(hospital)
.ajax({  
                type:”POST”,  
                url:”{:url(‘selectaccount’)}”,  
                dataType:”json”,  
                data:{‘hospital’:hospital},  
                success:function(e){  
                    //empty() 方法从被选元素移除所有内容
.ajax({                  type:”POST”,                  url:”{:url(‘selectaccount’)}”,                  dataType:”json”,                  data:{‘hospital’:hospital},                  success:function(e){                      //empty() 方法从被选元素移除所有内容
(“select[name=’account’]”).empty();
var html = “请选择账户“;
if(e.length == 1){
html += “” + e[0].name + ““;
}else{
(e).each(function (v, k) {  
                        html += “” + k.name + ““;  
                    });  
                    }  
                    //把遍历的数据放到select表里面
(e).each(function (v, k) {                          html += “” + k.name + ““;                      });                      }                      //把遍历的数据放到select表里面
(“select[name=’account’]”).append(html);
//从新刷新了一下下拉框
form.render(‘select’); //重新渲染
}
})
})
后台代码:
public function selectaccount(){
if(request()->isPost()) {
hospital=input(post.hospital);if(!empty( h o s p i t a l = i n p u t ( ‘ p o s t . h o s p i t a l ′ ) ; i f ( ! e m p t y ( hospital)){
session(‘hospitalid’, hospital); h o s p i t a l ) ; hospital_account = explode(“,”, session(‘hospital_id’));
foreach( hospitalaccountas h o s p i t a l a c c o u n t a s val){
hospitalids=explode("′′, h o s p i t a l i d s = e x p l o d e ( " " , val);
if( hospital== h o s p i t a l == hospital_ids[0])
accountids[]= a c c o u n t i d s [ ] = hospital_ids[1];
}
}
if(count( accountids)>1)$account=db(semaccount)>where(id,in,$accountids)>select();else$account=db(semaccount)>where(id,$accountids[0])>select();return a c c o u n t i d s ) > 1 ) $ a c c o u n t = d b ( ′ s e m a c c o u n t ′ ) − > w h e r e ( ′ i d ′ , ′ i n ′ , $ a c c o u n t i d s ) − > s e l e c t ( ) ; e l s e $ a c c o u n t = d b ( ′ s e m a c c o u n t ′ ) − > w h e r e ( ′ i d ′ , $ a c c o u n t i d s [ 0 ] ) − > s e l e c t ( ) ; r e t u r n account;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值