好的,我很簡單地試圖採取這個例子... http://jsfiddle.net/shanabus/HGF59/並把它放在我自己的服務器上的測試頁面上玩。我有另一個jquery的代碼,可以在另一個頁面上正常工作,我在這裏丟失了一些非常明顯的東西嗎?jquery語法錯誤
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.css" type="text/css" media="all" />
<link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
var areas = [{
value: "area1",
label: "Area 1",
desc: "Area 1 extended description"},
{
value: "sample2",
label: "Sample 2",
desc: "Sample of the second area"},
{
value: "third-area",
label: "Third Area",
desc: "This is the third area"}];
var projects = [{
value: "jquery",
label: "jQuery",
desc: "the write less, do more, JavaScript library",
id: 1
}, {
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery",
id: 2
}, {
value: "sizzlejs",
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine",
id: 3
}];
$("#areas").autocomplete({
minLength: 0,
source: areas,
select: function(event, ui) {
$("#areas").val(ui.item.label);
$("#project-div").show();
return false;
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo(ul);
};
$("#project").autocomplete({
minLength: 0,
source: projects,
focus: function(event, ui) {
$("#project").val(ui.item.label);
return false;
},
select: function(event, ui) {
$("#project").val(ui.item.label);
$("#project-id").val(ui.item.id);
$("#project-description").html(ui.item.desc);
alert("I could submit form with id " + ui.item.id);
return false;
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo(ul);
};
});
</script>
</head>
<body>
<div class="demo">
<label>Choose area:</label>
<input id="areas" />
<br />
<div id="project-div" style="display: none;">
<label>Choose project:</label>
<input id="project" />
<input type="hidden" id="project-id" />
<p id="project-description"></p>
</div>
</div>
</body>
</html>
該頁面名爲test.html,它位於與jquery-1.7.1.js相同的文件夾中。謝謝!
你能告訴我們問題究竟是什麼嗎? – Archer 2012-03-22 13:55:11
控制檯不顯示任何語法錯誤,那麼實際的問題是什麼?無論如何,這段代碼應該做什麼?需要更多信息。 – 2012-03-22 13:55:40
http://jsfiddle.net/HGF59/1/工作正常。 – Blazemonger 2012-03-22 13:55:52