2017-10-19 102 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