Loading

菜单联动,select联动菜单(搜索之后默认选中)

框架:thinkphp

php控制器代码:

$schedulelist = M('schedule')->getField('sid,schedule_name');
$this->assign('schedulelist',$schedulelist);

 

html代码:

<select name="sid" class="span2 m-wrap" id='sid' onchange="sidOnChange(this)">
     <option value="0">全部赛事</option>
     <php>$key=I('sid');$selected[$key]='selected';</php>
     <foreach name='schedulelist' item='value' key="key">
            <option value="{$key}">{$value}</option>
     </foreach>
</select>
<select name="match_id" class="span2 m-wrap" id='match_id'>
         <option value="0" {$selected['0']}>全部赛程</option>
</select>

jQuery代码:

jQuery(document).ready(function() {
           rs_change = 0;
           <php>$rs_sid = (I('sid'))?I('sid'):"-1";</php>
           if({$rs_sid}!='-1'){
               $('#sid').val({:I('sid')}).change().one('change',function(){
                       rs_change = 1;
               });
           }

        })
function sidOnChange(obj){
            var sid = $(obj).val();
            $.ajax({
                url:"{:U('/Admin/ticket/getMatch')}",
                data:{'sid':sid},
                type:'post',
                dataType:'json',
                success:function(res){
                    var text = '<option value="0">全部赛程</option>';
                    if($(res.data).length>0){
                        $.each(res.data, function(i, val) {  
                            text += "<option value='" + i + "'>" + val +"</option>";
                        });
                    }
                    $("#match_id").empty().append(text);
                    //没选过
                    if(rs_change==0){
                        $("#match_id").val({$rs_sid});
                    }
                }
            });
        }

接口代码:

public function getMatch(){
        $where['sid'] = I('post.sid');
        $res = M('match')->where($where)->getField('match_id,match_name');
        $this->ajaxReturn($res,"success",1);
    }

 

posted @ 2017-08-04 10:05  王召波  阅读(1679)  评论(0编辑  收藏  举报