简体   繁体   English

在select2中动态设置选项值

[英]set option value in select2 dynamically

I make a form. 我做一个表格。 In one of the input I'am using select2 ajax to search data on the database, then click it to the data you want to choose.The problem is when I submit the form, it get error because one of the input is null(that's the input in select option). 在我正在使用select2 ajax的输入中之一来搜索数据库中的数据,然后将其单击到您想要选择的数据上。问题是当我提交表单时,由于输入之一为null(即选择选项中的输入)。 I don't know why the value is still null, even I've clicked on the one of the option.Can anyone help me??? 我不知道为什么值仍然为null,即使我单击了选项之一。有人可以帮我吗??? Thank You :) 谢谢 :)

here's my view 这是我的看法

<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,
                    };
                }
            }
        });

Here's my controller 这是我的控制器

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');
    }
 }

My Model 我的模特

   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);
 }

Rewrite name="company" To name="Name" name="company"重写 name="Name"

OR 要么

Rewrite $this->form_validation->set_rules('Name', 'Name', 'required'); 重写$this->form_validation->set_rules('Name', 'Name', 'required'); To $this->form_validation->set_rules('company', 'Name', 'required'); $this->form_validation->set_rules('company', 'Name', 'required');

Do any one change will help you. 进行任何one更改都会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM