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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog