如何在codeigniter中从其他下拉框中选择一个选项时更改选择值

时间:2015-11-18 09:50:17

标签: php jquery html codeigniter

我有两个下拉框,其值是从实用程序库中获取的

    public $email_hooks = array(
    "create_user" => array(
            "name" => "New user creation",
            "keys" => array(
                "site_url" => "Site URL",
                "current_date" => "Current Date",
                "user_name" => "User name", 
                "name" => "Name of the user",
                "password" => "Password of user",
                "user_email"  => "Email address of new user"
            ),
            "content_type" => array("html","text"),
            "to" => array("User", "Site Admin"),
            "cc"    => array("Site Admin"),
            "bcc"   => array("Site Admin")
        ),
    "register_user" => array(
            "name" => "New user registration",
            "keys" => array(
                "site_url" => "Site URL",
                "current_date" => "Current Date",
                "user_name" => "User name", 
                "name" => "Name of the user",
                "activation_url" => "Url for activation registered account",
                "user_email"  => "Email address of the user"
            ),
            "content_type" => array("html","text"),
            "to" => array("User", "Site Admin","new user"),
            "cc"    => array("Site Admin"),
            "bcc"   => array("Site Admin")
        ));

以下是视图代码

            <div class="form-group col-lg-12">
            <label class="control-label col-lg-2">Name</label>
            <div class="col-lg-4">
                <select id ="choose" class="form-control" name="name">
                    <option value="">Select One</option>
                     <?php 
                        foreach($email_hooks as $key=>$val)
                        {
                            echo '<option value="'.$key.'">'.$val['name'].'</option>';
                        }
                    ?>

                </select>
            </div>
            <div class="col-lg-6" id="email_tmpl"></div>
            </div>

            <div class="form-group col-lg-12">
            <label class="control-label col-lg-2">Notification to</label>
            <div class="col-lg-10">
                <select id ="notification" class="form-control" name="notification">
                    <option value="">Select One</option>

                </select>
            </div>
            </div>

当选择第一个下拉框值时,我想更改第二个下拉值,即“to”=&gt; array(“User”,“Site Admin”,“new user”)。这些数组值根据第一个选择框而变化。

1 个答案:

答案 0 :(得分:0)

编写一个javascript / jquery函数,并通过java脚本或jquery从第一个下拉列表中获取值。然后使用onchange调用该函数,然后将该数组相应地附加到第二个下拉菜单。希望这有效......

你的第一次下拉:

<select id ="choose" class="form-control" name="name" onchange="your_javascript_function()">

使用javascript函数并使用jquery append函数及其中的每个函数:

 function your_javascript_function
 {$.each(your_array, function() {
        $('#notification').append( $('<option value="' + this.value_from_array + '">' + this.value_from_array + '</option>' ));
    });
  }