2010-10-09 93 views
0

我正在构建一个ASP.NET MVC网站,我需要一个标记编辑器,类似于Stack Overflow上使用的标记编辑器。我已经抬起头来如何做到与jQuery UI的自动完成必要的,但我已经遇到一个问题:当我将这个脚本放在外部.js文件,它不执行放置在外部脚本文件中时Javascript不会执行

这里是我的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文件中的一些合拢禁忌/大括号...

在此先感谢!

+0

你确定该文件的被包括在内?尝试将alert(“test”)'放在文件中,看看它是否触发。 – 2010-10-09 17:36:50

+0

@尼克好吧,会在一秒钟内测试。我应该把它放在'function bindAutoTagComplete(iteme,otherRootDomain){'或之前吗? **更新:**我已经尝试了两种方式;只有在函数声明工作之前。这意味着该文件正在加载,但由于某种原因,我的功能没有执行。是什么赋予了?谢谢你的帮助。 – 2010-10-09 17:37:58

+0

@Maxim - 在文件的最顶端工作...在基地的任何地方是有效的。 – 2010-10-09 17:38:49

回答

1

您需要的后一页准备附上事件。它当前运行#鸟不存在。

喜欢的东西

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

</script> 
+1

这是不正确,'$(函数(){'是一个快捷方式'$(文件)。就绪(函数(){',所以他已经催款这对'document.ready'。 – 2010-10-09 18:11:34

+0

这似乎解决问题!非常感谢 – 2010-10-09 18:12:46

+0

@Nick对我的作品 – 2010-10-09 18:13:24

相关问题