部分视图刷新后,Select2 multiple无法正常工作

时间:2017-05-17 06:59:01

标签: asp.net-mvc jquery-select2 partial-views

我是这个select2倍数的新手,
我有一个select2多个代码,用于在我的项目中选择多个,如下所示,

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   https://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
|   $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
|       my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'LoginN';
//$route['default_controller'] = 'IndexController';

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

这是在partrial视图中,它在fisrt时间工作正常,但当部分视图刷新时,我无法在框中键入任何内容,

我也尝试添加这行代码..

$('.itemName').select2({
    //data: data,
    placeholder: 'Select a Language',
    ajax: {
        url: '/User/LanguageSelect/',
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            //console.log(data);
            return {
                results: data
            };
        },
        cache: true
    }
});

但是当我添加这个时,select2也没有首先工作,我做错了什么?

这是我第一次加载的视图

First

并且在部分查看加载后我得到这样的

Second

请帮助

1 个答案:

答案 0 :(得分:0)

当你的部分视图被加载时(可能有一个事件),你必须(重新)绑定所有的jQuery对象。

就我所见,你已经走上了正确的轨道,但是第一个select2实例化是不同的,除此之外,我怀疑你没有挂钩到正确的事件(ajaxComplete

您可以分享加载PartialView的方式吗?

如果你不知道这些;只需将其添加到PartialView:

<script>
    $(function(){
        $('.itemName').select2({
            // add stuff
        });
    });
</script>