【问题标题】:i want to use multiple select2 onchange function in a dynamic way我想以动态方式使用多个 select2 onchange 函数
【发布时间】:2017-10-26 13:09:11
【问题描述】:

我想将 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

【问题讨论】:

    标签: javascript php jquery-select2


    【解决方案1】:

    在您的页面中插入 CSS 和 JS 文件,然后在 JS 文件之后在您的页面中插入此代码。

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

    要更新选项,请参见下面的代码。

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

    【讨论】:

    • 当我点击追加按钮时它会生成两个 select2....
    • @Failure 请检查CSS文件是否包含成功。
    • 第一个 select2 正在打开并且工作正常 onchange 功能...但是 secong select2 没有打开...它来自附加动态你能帮我吗??
    • @Failure 太好了,现在您只需要更新 select2 而不是再次初始化。
    • 如何更新 select2 ? $('.row-container:last').find("#new_").attr("id",locationId); 这里有什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2017-08-01
    • 2019-12-01
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多