放置在外部脚本文件中时,Javascript 不执行

发布于 2024-09-26 11:00:43 字数 2563 浏览 9 评论 0原文

我正在构建一个 ASP.NET MVC 网站,我需要一个标签编辑器,类似于 Stack Overflow 上使用的标签编辑器。我已经研究过如何使用 jQuery UI 完成必要的自动完成,但遇到了一个问题:当我将脚本放入外部 .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 文件中的一些右括号/大括号...

提前致谢!

I'm building an ASP.NET MVC site where I need a tag editor, similar to the one used on Stack Overflow. I've already looked up how to accomplish the necessary autocompletion with jQuery UI, but I've run into a problem: when I place the script in an external .js file, it doesn't execute.

Here is my 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> 

Here's 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;
            }
        });
    }

What do you think may be causing this problem? I'm probably missing some closing parantheses/braces in the .js file...

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-10-03 11:00:43

您需要在页面准备好后附加该事件。 #birds 当前运行时不存在。

像这样的东西

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

</script>

You need to attach that event after the page is ready. #birds doesn't exist when it runs currently.

Something like

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

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文