如果根据数据库a的值检查复选框,则jquery显示和隐藏下拉列表

时间:2016-06-09 02:58:43

标签: jquery html codeigniter

我正在尝试制作一个简单的jquery,带有1个复选框,下拉菜单中有4个选项。 使用这个HTML代码:

            <label>Busy</label>:</label>
            <br><br>
            <div class="checkbox checkbox-info checkbox-inline">
                <input type="checkbox" name="box" id="busy" <?=($restaurant_info["restaurant_busy"] != 0)? "checked":""?> / >
                <label for="busy">Busy</label>
            </div>
            <br><br>
                <div class="row">
                    <div class="col-md-6">
                        <select id="dropdownHolder" name="restaurant_busy">
                            <option value="0" <?php if ($restaurant_info["restaurant_busy"] == '0')  echo 'selected = "selected"'; ?>>No busy</option>
                            <option value="1" <?php if ($restaurant_info["restaurant_busy"] == '1')  echo 'selected = "selected"'; ?>>30 minute</option>
                            <option value="2" <?php if ($restaurant_info["restaurant_busy"] == '2')  echo 'selected = "selected"'; ?>>60 minute</option>
                            <option value="3" <?php if ($restaurant_info["restaurant_busy"] == '3')  echo 'selected = "selected"'; ?>>90 minute</option>
                            <option value="4" <?php if ($restaurant_info["restaurant_busy"] == '4')  echo 'selected = "selected"'; ?>>120 minute</option>
                        </select>
                    </div>
                </div>

这是我试图做的jquery,但我无法得到它

 $(function() {
    var selected = $('#dropdownHolder option:selected');
    $('#dropdownHolder').hide();
    $('#busy').change(function () {
        if ($('#busy').is(':checked')) {
            $('#dropdownHolder ').show();
        }else if (selected.val() !== "0") {
            $('#busy').prop('checked', true);
            $('#dropdownHolder ').show();
        }else{
            $('#busy').prop('checked', false);
            $('#dropdownHolder ').hide();
            }
        });
    });

这是我提交的表格

$("#edit_restaurant").submit(function(e){
    $("#edit_restaurant").attr('action', '/VENDOR/Vendor/change_restaurant/');
    e.preventDefault();
    this.submit();
});

我想如果,我的数据库中restaurant_busy的值为0,则不会选中该复选框,并且不会显示下拉菜单。我是jquery的新手,请帮帮我

比我想要的,如果未选中复选框,则复选框的值为0并将值发送到数据库,但我不知道如何制作它,所以我把值放在&#34; 0&#34;在下拉菜单中,你也可以帮我这个吗?

这是我的更新数据控制器

function change_restaurant(){

    if(!isset($_COOKIE["vendor_login"])){ redirect("/VENDOR",'refresh'); }
    if(!$this->Token_m->m_check_token($this->input->cookie('vendor_login'),$this->input->cookie('vendor_token'))){
        setcookie('vendor_login', '', time() - 3600, '/');
        redirect('/VENDOR/',"refresh");
    }
    if ($this->input->cookie('vendor_login') != null) {
        $admin_name = $this->input->cookie('vendor_login');

        setcookie('vendor_login', $admin_name, time() + 28800, '/');
        $msg = $this->input->cookie('vendor_token');
        setcookie('vendor_token', $msg, time() + 28800, '/');
    }

    if($vendorname = $_COOKIE["vendor_login"]) {

        $check_login = $this->Vendor_m->m_get_user_by_vendor($vendorname);
        $restaurant_id = $check_login["restaurant_id"];
        {

            if ($_POST == NULL){
                redirect("/VENDOR/Vendor/vendor_setting","refresh");
            }
            $data = array(
                "restaurant_busy" =>$this->input->post("restaurant_busy"),
                "restaurant_active" =>$this->input->post("restaurant_active"),
                "delivery_active" =>$this->input->post("delivery_active"),
                "takeaway_active" =>$this->input->post("takeaway_active"),
                "voucher_active" =>$this->input->post("voucher_active"),

            );
            $this->Vendor_m->m_update_restaurant_info($data,$restaurant_id);
        }
    }redirect("/VENDOR/Vendor/vendor_setting","refresh");
}

非常感谢,对不起,如果我的请求太多,你可以选择你想要回答的内容

1 个答案:

答案 0 :(得分:1)

Lemme知道这对你有用吗;)

$(function() {
    var selected = $('#dropdownHolder option:selected'), // Seems to be unused
        $busy = $('#busy'), // Always cache your queries
        $dropdown = $('#dropdownHolder'); // Caching queries

    $dropdown.hide(); // Hidden by default initially

    $busy.change(function () {
        if ($busy.prop('checked')) {
            $dropdown.show().focus().click();
        } else{
            $busy.prop('checked', false);
            $dropdown.blur().hide();
        }
    });

    $busy.change(); // This sets initial state
});