如何在codeigniter中编写jquery [英] How to code jquery in codeigniter

查看:88
本文介绍了如何在codeigniter中编写jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器和视图。我试图在codeigniter中学习jquery。

I have the following controller and view. I am trying to learn jquery in codeigniter.

代码不工作。

提前感谢。

class PhpJqueryBook extends Controller
{

function __construct()
{
    parent::Controller(); }

public function index()
{
...    }

function dynamic_select_boxes(){

    $this->load->view('dynamic_select_boxes');

}

function get_cities(){

    switch(@$_POST['country']){
        case 'ie': // { ireland
            $cities=array('Cork', 'Dublin', 'Galway', 'Limerick',
              'Waterford');
        break;
        // }
        case 'uk': // { United Kingdom
            $cities=array('Bath', 'Birmingham', 'Bradford',
                'Brighton & Hove', 'Bristol',
                'Cambridge', 'Canterbury', 'Carlisle',
                'Chester', 'Chichester', 'Coventry',
                'Derby', 'Durham', 'Ely', 'Exeter',
                'Gloucester', 'Hereford', 'Kingston upon Hull',
                /* and on and on! */
                'Newport', 'St David\'s', 'Swansea');
        break;
        // }
        default: // { else
            $cities=false;
        // }
    }
    if(!$cities)echo 'please choose a country first';
    else echo '<select name="city"><option>'.join('</option>  <option>',$cities).'</select>';
}
}

views / dynamic_select_boxes.php

views/dynamic_select_boxes.php

<?php $this->load->view('inc/header')?>

<form>
<table>
<tr><th>Country</th><td>
<select name="country" id="country">
<option value=""> -- please choose -- </option>
<option value="ie">Ireland</option>
<option value="uk">Great Britain</option>
</select>
</tr>
<tr>
<th>Cities</th>
<td id="cities">please choose a country first</td>
</tr>
</table>
<?php $this->load->view('inc/footer')?>

这会生成以下html。

And this produces the following html.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <base href="http://127.0.0.1/ci_jquery/">
</head>
<body>

<form>
<table>
    <tr><th>Country</th><td>
    <select name="country" id="country">
    <option value=""> -- please choose -- </option>
    <option value="ie">Ireland</option>
    <option value="uk">Great Britain</option>
    </select>
    </tr>
    <tr>
    <th>Cities</th>
    <td id="cities">please choose a country first</td>
</tr>
    </table>
   <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>


<script type="text/javascript">
    $(document).ready(setup_country_change);
        function setup_country_change(){
            $('#country').change(update_cities);
        }
        function update_cities(){
            var country=$('#country').attr('value');
            $.get('phpjquerybook/get_cities/'+country, show_cities);
        }
        function show_cities(res){
            $('#cities').html(res);
        }       
        </script>


</body>
</html>


推荐答案

要获得控制器方法中的第三个参数你可以很容易地使用方法的参数来获取这些,或者通过uri类来获取段。在第一种情况下,您将使用

To get the third parameter in a method of a controller you can easily use the parameters of the method to get these, or the uri class to get the segments. In the first case you would use

function get_cities($country = null){

    switch($country){

    ....

请参阅codeigniter userguide for < a href =http://codeigniter.com/user_guide/general/controllers.html#passinguri =nofollow noreferrer>将uri参数传递给控制器​​和 uri库

see codeigniter userguide for passing uri parameters to controllers and uri library.

这篇关于如何在codeigniter中编写jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆