簡體   English   中英

在select2中動態設置選項值

[英]set option value in select2 dynamically

我做一個表格。 在我正在使用select2 ajax的輸入中之一來搜索數據庫中的數據,然后將其單擊到您想要選擇的數據上。問題是當我提交表單時,由於輸入之一為null(即選擇選項中的輸入)。 我不知道為什么值仍然為null,即使我單擊了選項之一。有人可以幫我嗎??? 謝謝 :)

這是我的看法

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
  </head>
  <body>
   <?php echo form_open('company2/masuk') ?>
  <form>
  <select id="Name" class="searching form-control" style="width:500px" name="company"></select>
 <button type="submit" class="btn btn-info waves-effect waves-light">Save</button>
 <?php echo form_close(); ?>
 </form>
  </body>
 <script type="text/javascript">
  $('.searching').select2({
            placeholder: 'Masukkan Nama Company',
            ajax:{
                url: "<?php echo base_url('company2/select2'); ?>",
                dataType: "json",
                delay: 250,
                processResults: function (param) {
        return {
          compClue: param.term,

        };
      },
      processResults: function(data){
                    var results = [];

                    $.each(data, function(index, item){

                        results.push({
                            id: item.Name,
                            text: item.Name,
                            value:item.Name
                        });
                    });
                    return{
                        results: results,
                        cache: true,
                    };
                }
            }
        });

這是我的控制器

public function masuk(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url_helper');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_rules('CreatedOn', 'CreatedOn', 'required');
$this->form_validation->set_rules('Status', 'Status', 'required');
    if ($this->form_validation->run() == FALSE) {
  $this->add_company();

  }else{
  $this->company_model->buat();
    redirect('company2/index');
    }
 }

我的模特

   public function buat(){
$data = array(
  'Name' => $this->input->post('Name'),
  'CreatedOn' => $this->input->post('CreatedOn'),
  'Status' => $this->input->post('Status')
);
return $this->db->insert('dbo.Company', $data);
 }

name="company"重寫 name="Name"

要么

重寫$this->form_validation->set_rules('Name', 'Name', 'required'); $this->form_validation->set_rules('company', 'Name', 'required');

進行任何one更改都會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM