Javascript не выполняется при размещении во внешнем файле скрипта - PullRequest
0 голосов
/ 09 октября 2010

Я создаю сайт ASP.NET MVC, где мне нужен редактор тегов, похожий на тот, который используется при переполнении стека. Я уже посмотрел, как выполнить необходимое автозаполнение с помощью пользовательского интерфейса jQuery, но столкнулся с проблемой: , когда я помещаю скрипт во внешний файл .js, он не выполняется .

Вот мой test.html:

<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Test</title> 
    <script src="http://jqueryui.com/jquery-1.4.2.js"></script> 
    <script src="http://jqueryui.com/ui/jquery.ui.core.js"></script> 
    <script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script> 
    <script src="http://jqueryui.com/ui/jquery.ui.position.js"></script> 
    <script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
    <script src="jquery.tagautocomplete.js"></script>
    <script> 
    $(function() { bindAutoTagComplete('#birds'); })
    </script>
</head> 
<body> 
    <label for="birds">Birds: </label> 
    <input id="birds" size="50" /> 
</body> 
</html> 

Вот jquery.tagautocomplete.js:

function bindAutoTagComplete(item, otherRootDomain)
{
        function split( val ) {
            return val.split( / \s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $(item).autocomplete({
            source: function( request, response ) {
                $.getJSON('http://jqueryui.com/demos/autocomplete/search.php', {
                    term: extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( " " );
                return false;
            }
        });
    }

Как вы думаете, что может быть причиной этой проблемы? Возможно, мне не хватает некоторых закрывающих скобок / скобок в файле .js ...

Заранее спасибо!

1 Ответ

1 голос
/ 09 октября 2010

Вам необходимо прикрепить это событие после того, как страница будет готова.#birds не существует, когда он работает в данный момент.

Что-то вроде

<script>
$(document).ready( function(){  bindAutoTagComplete('#birds'); } );

</script>
...