2014-11-04 30 views
0

我一直作爲此行的js的問題中提及接收錯誤:如何解決此錯誤:無法讀取未定義的屬性'selectize'?

var $filter = $('#share_tags')[0].selectize; 

的相關HTML也一起附着..

(function() { 
 
\t // Extra symbols for our share expression 
 
\t var symbols = ['+','|','~','(',')'], 
 
\t \t symbolCount = {}; 
 
\t // Initialize all symbols count to 0 
 
\t symbols.forEach(function(s) { 
 
\t \t symbolCount[s] = 0; 
 
\t }); 
 
\t // our ops 
 
\t var ops = [ 
 
\t \t { value: '+', text: 'AND', symbol:true }, 
 
\t \t { value: '|', text: 'OR', symbol: true }, 
 
\t \t { value: '~', text: 'NOT', symbol: true }, 
 
\t \t { value: '(', text: '(', symbol: true }, 
 
\t \t { value: ')', text: ')', symbol: true } 
 
\t ]; 
 

 
\t function getText(value) { 
 
\t \t for(var i = 0, len = ops.length; i < len; i += 1) { 
 
\t \t \t if (ops[i].value === value) { 
 
\t \t \t \t return ops[i].text; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
\t // User select 
 
\t $('#share_with').selectize({ 
 
\t \t openOnFocus: false, 
 
\t }); 
 

 
\t // Tag select 
 
\t $('#share_tags').selectize({ 
 
\t \t persist: false, 
 
\t \t onItemAdd: function(value, $item) { 
 
\t \t \t if (symbolCount.hasOwnProperty(value[0])) { 
 
\t \t \t \t var value = value[0], 
 
\t \t \t \t \t count = symbolCount[value]; 
 
\t \t \t \t $filter.addOption({ 
 
\t \t \t \t \t value: value + '' + (count+1), 
 
\t \t \t \t \t text: getText(value) 
 
\t \t \t \t }); 
 
\t \t \t \t symbolCount[value] += 1; 
 
\t \t \t \t $filter.refreshOptions(); 
 
\t \t \t } 
 
\t \t }, 
 
\t \t onItemRemove: function(item) { 
 
\t \t \t if (symbolCount.hasOwnProperty(item[0]) !== -1) { 
 
\t \t \t \t for (opt in $filter.options) { 
 
\t \t \t \t \t if ($filter.options.hasOwnProperty(opt)) { 
 
\t \t \t \t \t \t if (opt !== item && opt[0] === item) { 
 
\t \t \t \t \t \t \t delete $filter.options[opt]; 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t }); 
 

 
\t var $filter = $('#share_tags')[0].selectize; 
 

 
\t $('#shareForm').on('submit', function(e) { 
 
\t \t var tags = $('#share_tags').val(), 
 
\t \t \t expression = [], 
 
\t \t \t symbol, 
 
\t \t \t item; 
 

 
\t \t for (var i = 0, len = tags.length; i < len; i += 1) { 
 
\t \t \t item = tags[i]; 
 
\t \t \t if (symbolCount.hasOwnProperty(item[0]) !== -1) { 
 
\t \t \t \t symbol = item[0] === '+' ? '&' : item[0]; 
 
\t \t \t \t expression.push(symbol); 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t expression.push(item); 
 
\t \t \t } 
 
\t \t } 
 
\t \t $('#share_expression').val(expression.join('')); 
 
\t }); 
 

 
\t 
 
    $(document).keyup(function(e) { 
 
    if (e.keyCode == 27) { 
 
     $(".search-card").removeClass("hide"); 
 
    } // esc 
 
    }); 
 

 
    $('.js-share-btn').on('click', function(e) { 
 
     $(".search-card").addClass("hide"); 
 
     App.Util.preventDefault(e); 
 
     Avgrund.show("#createShare"); 
 
    }); 
 

 
    $('.edit-share-button').on('click', function(e) { 
 
     $(".search-card").addClass("hide"); 
 
     App.Util.preventDefault(e); 
 
     Avgrund.show("#createTaskShare");  
 
     }); 
 
    
 
    $(".js-popup-close").on("click",function() { 
 
     $(".search-card").removeClass("hide"); 
 
    }); 
 
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
{% set created_tags = [t for t in tags if t.tag.created_by == current_user] %} 
 
<form name="shareForm" id="shareForm" method="post" action="/share/new"> 
 
\t <fieldset> 
 
\t \t <h3>Share card</h3> 
 
\t </fieldset> 
 
\t <div class="form-group"> 
 
\t \t <label for="share_name">Card name</label> 
 
\t \t <input type="text" class="form-control" name="share_name" id="share_name" required="required"> 
 
\t </div> 
 
\t <div class="form-group"> 
 
\t \t <label for="share_tags">Tag</label> 
 
\t \t <select class="form-control" name="share_tags" id="share_tags" required="required" placeholder="Pick a tag to share"> 
 
\t \t \t {% if q[0] == '#' %} 
 
\t \t \t {% set name, id = q.split("/") %} 
 
\t \t \t <option value="{{ id }}">{{ name }}</option> 
 
\t \t \t {% end %} 
 
\t \t </select> 
 
\t \t {# 
 
\t \t <select class="form-control" name="share_tags" id="share_tags" required="required" multiple="multiple" placeholder="Pick a tag to share"> 
 
\t \t \t <option value=""></option> 
 
\t \t \t <optgroup label="Tags"> 
 
\t \t \t \t {% for t in created_tags %} 
 
\t \t \t \t <option value="{{ t.tag.id }}">{{ t.tag.name }}</option> 
 
\t \t \t \t {% end %} 
 
\t \t \t </optgroup> 
 
\t \t \t <optgroup label="Filters"> 
 
\t \t \t \t <option value='+'>AND</option> 
 
\t \t \t \t <option value='|'>OR</option> 
 
\t \t \t \t <option value='~'>NOT</option> 
 
\t \t \t \t <option value='('>(</option> 
 
\t \t \t \t <option value=')'>)</option> 
 
\t \t \t </optgroup> 
 
\t \t </select> 
 
\t \t #} 
 
\t </div> 
 
\t <div class="form-group"> 
 
\t \t <label for="share_with">Share with</label> 
 
\t \t <select class="input-control" name="share_with" id="share_with" required="required" multiple="multiple" placeholder="Choose a few people"> 
 
\t \t \t <option value="">Select a user</option> 
 
\t \t \t {% for user in contacts %} 
 
\t \t \t \t {% if user.id != 1 %} 
 
\t \t \t \t <option value="{{ user.id }}">{{ user.name }} &lt;{{ user.email }}&gt;</option> 
 
\t \t \t \t {% end %} 
 
\t \t \t {% end %} 
 
\t \t </select> 
 
\t </div> 
 
\t {% module xsrf_form_html() %} 
 
\t <input type="hidden" name="share_expression" id="share_expression" value=""> 
 
\t <div class="form-group"> 
 
\t \t <button type="submit" class="btn btn-primary share-btn">Share</button> 
 
\t \t <button type="button" class="btn-link js-popup-close">Cancel</button> 
 
\t </div> 
 
</form>

回答

-1

Selectize是一個jQuery插入。如果你不導入它,你將無法使用它。 (這是錯誤,您的對象沒有選擇性屬性)​​。你可以看到plugin page,或者它是github

下載它here並嘗試它。

記得添加以下行:

<script type="text/javascript" src="selectize.js"></script> 
<link rel="stylesheet" type="text/css" href="selectize.css" /> 
相關問題