我想以动态方式使用多个select2 onchange函数

时间:2017-10-26 13:09:11

标签: javascript php jquery-select2

我想在我的代码中放入select2函数,但不知道将.select2()函数放在何处 我有这个.append($(<?php echo json_encode($property_address1); ?>)) 我的模特`

   function property_address1()
   {
     $query = $this->db->query('SELECT host,price,city,property_thumbnail, apartments_type, contactnumber, contactperson,photographlinks,emailid, propertyaddress FROM tbl_contacts')->result();
     $output = '<select id="neww" class="property_add_ form-control">';


    foreach ($query as $row) 
    {
        //echo $row->location;
        $output .= "<option value='". $row->propertyaddress ."'";
        $output .= " data-propertyaddress='" . $row->propertyaddress ."'" ;
        $output .= " data-host_name='" . $row->host ."'" ;

        $output .= " data-apartments_type ='" . $row->apartments_type."'" ;

        $output .= " data-city ='" . $row->city."'" ;

        $output .= " data-property_thumbnail='" . $row->property_thumbnail."'" ;

        $output .= " data-price='" . $row->price."'" ;

        $output .= " data-contactperson='" . $row->contactperson ."'" ;
        // $output. = $row->pincode.", ".$row->city.", ".$row->location;
        $output .= " data-photographlinks='" . $row->photographlinks ."'" ;
        $output .= " data-emailid='" . $row->emailid ."'" ;
        $output .= " data-contactnumber='". $row->contactnumber . "'>" ;
        $output .=   $row->host . ' , '.$row->propertyaddress . ' ,'.$row->price. ' ,'.$row->apartments_type. ' , '. $row->contactperson . ' , ' . $row->contactnumber. "</option>";
    }
    $output .= '</select>';
    //var_dump($output);
    return $output;

    }`

我的控制器

  public function test($id = null)
    {
        $this->layout->set(
                array(
                    'property_address1' => $this->mdl_quotes->property_address1()
                     )
                 );
        $this->load->model('mdl_quotes');
        $this->layout->buffer('content', 'quotes/test'); 
        $this->layout->render();
    }

Dhaval Panchal check this updated screenshot

This is screenshot for better understanding.. please have a look Thanks

1 个答案:

答案 0 :(得分:0)

插入CSS&amp; JS文件在页面中插入此代码后,页面中的JS文件。

<script type="text/javascript">
    $(document).ready(function() {
        $('#neww').select2();
    });
</script>

要更新选项,请参阅以下代码。

var option = new Option("text", "id");
$("#neww").append(option);
$("#neww").trigger("change");