2017-10-19 100 views
-4

我写这个剧本JQuery脚本与多个其他脚本的位置?

$.ajax({ 
    type: "POST", 
    url: "/my/GetTagsByID", 
    data: { 
     Link_ID: LId 
    }, 
    datatype: "json", 
    success: function(result) { 
     $('#tokenfield-typeahead').val('Success 1, Success 2') 
    }, 
    error: function(jqXHR) { 
     alert(jqXHR.status); 
    } 
}) 

问题是,但我不知道应该是什么的SCRIPTS

的确切位置。当我了它这样:

<script src="~/Scripts/jquery-1.10.2.js"></script> 
<script src="~/Scripts/dist/typeahead.bundle.js"></script> 
<link href="~/Scripts/dist/css/tokenfield-typeahead.css" rel="stylesheet" /> 
<script src="~/Scripts/dist/bootstrap-tokenfield.js"></script> 
<link href="~/Scripts/dist/css/bootstrap-tokenfield.css" rel="stylesheet" /> 

的结果如下 enter image description here

这意味着,我可以使用TokenFieldTypeAhead但它并没有取代值$('#tokenfield-typeahead').val('Success 1, Success 2')这意味着我无法访问jQuery?

但是,当我为Jquery脚本切换位置,这个(移动jQuery来底部):

<script src="~/Scripts/dist/typeahead.bundle.js"></script> 
<link href="~/Scripts/dist/css/tokenfield-typeahead.css" rel="stylesheet" /> 
<script src="~/Scripts/dist/bootstrap-tokenfield.js"></script> 
<link href="~/Scripts/dist/css/bootstrap-tokenfield.css" rel="stylesheet" /> 
<script src="~/Scripts/jquery-1.10.2.js"></script> 

我得到下面的结果 enter image description here

我不能使用Tokenfield,我不能使用TypeAhead了。

我该怎么办?

增加了新的代码 我已经添加了新的代码,它似乎像jQuery是无法识别tokenfiled作为一个功能 - 我怎样才能解决这个地方我已经添加了所有必需的引用 - 请参见下面的代码(我越来越控制台同样的错误,不管在哪里我测试) - 请运行该代码,然后单击按钮

$('#bt').click(function() { 
 

 
    $('#tokenfield-typeahead').tokenfield('setTokens', ['blue', 'red', 'white']); 
 

 
});
<link href="http://sliptree.github.io/bootstrap-tokenfield/dist/css/bootstrap-tokenfield.css" rel="stylesheet" /> 
 
<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
 
<script src="http://sliptree.github.io/bootstrap-tokenfield/dist/bootstrap-tokenfield.js"></script> 
 

 

 

 
<input type="text" class="form-control" id="tokenfield-typeahead" value="Temp 1, Temp 2" data-limit="10" placeholder="Enter tags" /> 
 

 
<button id="bt">Click me</button>

enter image description here

回答

0

找到了解决办法 - 感谢TJ到通知运行控制台脚本

是,Bootstrap.min.js需要加载before Tokenfiled(JS)的问题 - 这么做其实就是固定的问题,现在JQuery的能将Tokenfield识别为功能并按预期工作。我仍然没有理由为什么Bootstrap没有加载,因为我已经在我的_Layout页面和Bundle中。在任何情况下,我会看到我怎样才能重新安排我的脚本包 -

感谢

4

...which means I cannot access jQuery?

不,这并不意味着根本就没有。这仅表示val没有达到您的预期。

这并不让我感到意外。它可能是极其您使用的tokenfield插件可能需要您使用特定于标记字段的方法来设置值,而不是val。在the documentation扫视了一眼,这似乎是tokenfield('setTokens', ...)

$('#tokenfield-typeahead').tokenfield('setTokens', 'Success 1,Success2'); 

What am I supposed to do?

把jQuery的任何插件之前,由于插件需要jQuery的在那里为了挂钩投案自首;当事情不能按预期工作时,始终将文档作为您的第一个操作;并使用正确的方法设置令牌字段中的“值”。

+0

我又显得很深刻,试图做到这一点'$(“#tokenfield-预输入”)tokenfield('setTokens。 ''['blue','red','white']);'但是仍然没有运气 – aliusman

+1

@aliusman:请用一个** runnable ** [mcve]更新您的问题,使用Stack Snippets(' [<>]'toolbar button),确保链接所提供的列表或CDN中的所有库(如https://cdnjs.com/)。 –

+0

我得到了以下错误:(“消息”:“未捕获的SyntaxError:失踪)参数列表后”, – aliusman