设置负载的初始值与选择二阿贾克斯 [英] Setting initial values on load with Select2 with Ajax

查看:296
本文介绍了设置负载的初始值与选择二阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了与阿贾克斯(有单个或多个)选择2的控制。我想对页面加载的一些价值观,但我不能得到这可是工作。我的$ C $下选择2给出如下:

I have select2 controls set up with Ajax (have both single and multiple). I'm trying to have some values on page load but I'm not able to get this to work however. My code for select2 is given below:

function AjaxCombo(element, url, multival ){  // multival = true or false
  multival = multival || false;
  $(element).select2({
   minimumInputLength: 2,
   multiple: multival,
   separator: '|',
   ajax: {
     url: url,
     dataType: 'json',
     data: function (term, page) {
        var targetname = $(this).attr('name');
        var target = targetname.slice(targetname.indexOf("[")+1, targetname.indexOf("]"));
       return {
         targettype: "search",
         target: target,
         search: term
       };
     },
     results: function (data, page) {
       return { results: data };
     }
   }
 });
}
AjaxCombo(".ajaxselect", "includes/linkedcontrol.php", false);
AjaxCombo(".ajaxmultiselect", "includes/linkedcontrol.php", true);

阿贾克斯组合工作得很好,我有麻烦只与初始值的负载。我试过低于这个code,但无法得到它的工作:

The Ajax combo works fine, am having trouble only with the initial values load. I tried this code below but couldn't get it to work:

initSelection : function (element, callback) {
    var elementText = $(element).attr('data-initvalue');
    callback(elementText);
}

我从PHP的HTML返回如下:

My HTML from php is returned as below :

<input name='header[country]' class='ajaxselect'  data-initvalue='[{"id":"IN","name":"India"}]' data-placeholder='Select Country' value='' />

我看到值从PHP人口,只有我的jQuery是有问题。我在控制值显示为我们|美国。我想我已经为获得这一格式作为默认不使用格式选项编辑选择二源。

I see that values are populated from php, only my jquery is having issues. My values in the control show up as US | United States of America. I guess I have edited the select2 source for getting this format as default without using format option.

任何人都可以请帮我填充的默认值?先谢谢了。

Can anyone please help me populate the default values? Thanks in advance.

推荐答案

为其指定为 initSelection 的函数调用初始价值作为参数。因此,如果是空的,功能不叫。
当你specifiy 值='[{ID:IN,名:印度}] 而不是数据initvalue 函数被调用和选择可以得到初始化。

The function which you are specifying as initSelection is called with the initial value as argument. So if value is empty, the function is not called.
When you specifiy value='[{"id":"IN","name":"India"}]' instead of data-initvalue the function gets called and the selection can get initialized.

这篇关于设置负载的初始值与选择二阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆