【发布时间】:2010-10-09 17:33:46
【问题描述】:
我正在构建一个 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 文件中遗漏了一些右括号/大括号...
提前致谢!
【问题讨论】:
-
您确定该文件已包含在内吗?尝试将
alert("test")放在文件中,看看它是否会触发。 -
@Nick 好的,稍后将进行测试。我应该把它放在
function bindAutoTagComplete(iteme,otherRootDomain) {之后还是之前? 更新:两种方法我都试过了;只把它放在函数声明工作之前。这意味着文件正在加载,但由于某种原因,我的函数没有执行。是什么赋予了?感谢您的帮助。 -
@Maxim - 在文件的最顶部工作......在基地的任何地方都是有效的。
-
其他信息:当将该代码放入 HTML 页面上的
<script>块时,它会执行并且我在 FireBug 中看到对search.php的请求。但是,当它像这样设置时,不会出现任何请求。 -
@Maxim -
.js文件在哪里?按照您的方式,它必须与网页位于同一目录中。
标签: javascript jquery jquery-ui scripting external-script