使用jquery codeigniter检查/取消选中切换按钮时动态显示和隐藏链接

时间:2016-09-13 07:31:39

标签: javascript jquery codeigniter

我创建了一个类别模块和开关按钮然后链接显示和切换按钮关闭然后链接隐藏。我正在使用codeigniter framemwork。

控制器:

 public static void ReadData(out Turistai[] tourists, out int amount)
    {
        amount = 0;
        tourists = new Turistai[MaxTourists];
        using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P2\\P2.1\\turistai.csv"))
        {
            string line = null;
            while( (line = reader.ReadLine()) != null) 
            {
                string[] values = line.Split(';');

                string name = values[0];
                int euros = int.Parse(values[1]);
                int cents = int.Parse(values[2]);
                Console.WriteLine(euros);
                //Turistai tourists = new Turistai(name, euros, cents);
                amount++;
            }
        }
    }

查看:

class Category extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category_model');
        $this->load->library('form_validation');
    }

 public function index() {
        if (!empty($_SESSION['admin'])) {
            $data = array(
                'page_title' => 'Category',
                'page_name' => 'category/index',
                'result' => $this->category_model->list_all()
            );
            $this->load->view('template', $data);
        } else {
            redirect('login');
        }
    }

 public function update_cat_status() {
        $data = $this->category_model->update_cat_status($_GET);
        echo json_encode($data);
    }

Jquery的

<table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th>Category Name</th>
                                    <th>Status</th>
                                    <th>Edit</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($result as $value): ?>
                                    <tr>
                                        <td><?= $value['name']; ?></td>
                                        <td> <div class="onoffswitch">
                                                <input type="hidden" value="<?= $value["id"]; ?>"/>
                                                <input type="checkbox" class="js-switch"
                                                <?php
                                                if ($value['status'] == 1) {
                                                    echo "checked";
                                                }
                                                ?>>
                                            </div>
                                        </td>
                                        <td><a href="<?= base_url("category/edit/{$value['id']}"); ?>" class="btn btn-success">Edit</a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>

Screenshot

我的问题: 开关按钮然后编辑按钮隐藏并关闭开关按钮然后编辑按钮显示。 所以请查看上面的截图

1 个答案:

答案 0 :(得分:0)

  1. 在视图中为您的按钮添加id属性:
  2. 来自<a href="<?= base_url("category/edit/{$value['id']}"); ?>" class="btn btn-success">Edit</a>

    至:<a id="btnEdit<?= $value['id']?>" href="<?= base_url("category/edit/{$value['id']}"); ?>" class="btn btn-success">Edit</a>

    1. 关于你的jquery:
    2.   

      if($(this).children(':checked'))

      {
       var status = 1;
       $("#btnEdit"+cat_id).show();
      }
      else
      {
       var status = 0;
       $("#btnEdit"+cat_id).hide();
      }