duanhu7400 2013-12-11 09:16
浏览 21
已采纳

三个基于先前的jquery选择每个

I'm trying to do 3 selects in cakePhp + jQuery first one with provinces, second - localities, third - schools in that place. Here is my cake code (so far):

           echo $this->Form->input('proviences', array(
                'type' => 'select',
                'empty' => true,
                'options' => $proviences,
                'label' => 'Province',
                'class' => 'proviences',
                'before' => '<div style="float:left;width:180px"',
                'after' => "</div>"
            ));

            echo $this->Form->input('localities', array(
                'type' => 'select',
                'empty' => true,
                'options' => $localities,
                'label' => 'City',
                'class' => 'localities',
                'before' => '<div style="float:left;width:180px"',
                'after' => "</div>"
            ));
            $schoolList = array();
            foreach($schools as $value) {
                $schoolsList[]=$value['name'];
            }
            echo $this->Form->input('school_id', array(
                'label' => 'Szkoła',
                'options' => $schoolsList,
                'empty' => true,
                'before' => '<div style="float:left;width:240px"',
                'after' => "</div>",
                'onchange' => "submit();",
            ));

In $schools i have a list looking this way

array(
   id1 => array(
         'name' => 'some_name',
         'province' => 'some_province',
         'locality' => 'some_city'
         )
)

and using this to get lists of provinces,localities and school names I was trying to use this but couldn't get it working ;/ Filter three select boxes based on previous selections Is there a way of doing it in jQuery without ajax?

  • 写回答

1条回答 默认 最新

  • duangao8359 2013-12-11 10:10
    关注

    you can do it without using ajax, but for that you would need the schools array on client side, and create/edit options based on that on the client side, too.

    here is a working fiddle. (hastily thrown together, but you get the idea). You need to get the schools array into js, though. you could either use AJAX for that or, pass it as json on the page:

    echo 'var schools = JSON.parse('.json_encode($schools).');
    

    You have to think about where to place that, too, so that the variable doesn't leak into the global scope. you could put it in the jQuery closure, for example:

    echo '(function($){';
    echo 'var schools = JSON.parse('.json_encode($schools).');
    
    // now the javascript from the fiddle...
    
    echo '}(jQuery))';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计