jquery tokenInput如何动态地将ajax调用url更改为脚本

Pin*_*wer 2 ajax jquery jquery-tokeninput

我正在使用这个强大而美观的插件http://loopj.com/jquery-tokeninput/,但我遇到了一个问题.

有没有办法在我的tokenInput中动态更改或更改ajax调用的URL.

所以举个例子.

jQuery("#selector").tokenInput("http://mysite.com?name=John");
Run Code Online (Sandbox Code Playgroud)

当我点击我页面上的按钮时,我想将网址更改http://mysite.com?name=Johnhttp://mysite.com?name=Adam

所以整个逻辑就像这样:

jQuery("#myButton").click(function(){
    //Change the URL of the jQuery("#selector").tokenInput();
});
Run Code Online (Sandbox Code Playgroud)

起初我所做的就是这样:

jQuery("#myButton").click(function(){
        jQuery("#selector").tokenInput("http://mysite.com?name=Adam");
});
Run Code Online (Sandbox Code Playgroud)

这样做的问题是它创建了一个类的副本token-input-list-facebook,只是为了看看我在说什么.

从这样:

在此输入图像描述

在此输入图像描述

如您所见,它复制了textinput或类token-input-list-facebook.

我只是向您展示我在我的应用程序中尝试做的简单逻辑.

有办法怎么做那个?非常感谢您的帮助和奖励!:-)

顺便说一句,我正在使用facebook主题,这就是类名的原因token-input-list-facebook.

LeG*_*GEC 5

从该组件的代码来看:您可以提供一个返回字符串的函数,而不是提供字符串作为 url。所以你应该能够写:

function setupMyTokenInput($input, $btn){
   //this "protected" var will hold the target url
   var url = 'http://mysite.com?name=John';

   $input.tokenInput( 
   //instead of a string, give a function which returns the variable's value
   function(){
     return url;
   });

   $btn.click(function(){
       //clicking the button changes the var's value
       url = 'http://mysite.com?name=Adam';
   });
}

$(function(){
   setupMyTokenInput( $('#selector'), $('#button') );
});
Run Code Online (Sandbox Code Playgroud)

我还没有尝试过,但与此接近的东西应该可以工作。


Pin*_*wer 5

我自己解决了.这就是我做的.

jQuery("#selector").tokenInput("http://mysite.com?name=John");

jQuery("#myButton").click(function(){
    jQuery(".token-input-list-facebook").remove();
    jQuery("#selector").tokenInput("http://mysite.com?name=Adam");
});
Run Code Online (Sandbox Code Playgroud)

我只是remove()这个类,token-input-list-facebook因为每次初始化tokenInput时都会添加类.然后再次使用所需的ajax URL重新初始化tokenInput.

太糟糕了,我没有在TokenInput中看到关于如何做到这一点的好文档.