我正在尝试传递自己的对象数组:results[num_row] = {'title:\'' + title + '\', ' + 'url:\'' + url + '\''};
但这会在firebug中返回错误
当我尝试:results[num_row] = {title:'Link A', url:'/page1'}
它有效。
谢谢,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript">
var test = ["a","b","ab"];
var results = new Array();
function prep(){
$("#searchbox").autocomplete(results,{
formatItem: function(item) {
return item.title;
}
}).result(function(event, item) {
location.href = item.url;
});
}
$(document).ready(function(){
$.ajax({
type: "GET",
url: "links2.xml",
dataType: "xml",
success: function(xml) {
// Count elements
var count = $(xml).find('ROW').length;
// Create Array of correct length
//window.results = new Array(count);
// Set array variable
var num_row = 0;
//data string
var datastring = "";
//start of find block
$(xml).find('ROW').each(function() {
var title = $(this).find('SC_DF_FIELD_1').text();
var url = $(this).find('SC_DF_FIELD_2').text();
var support_url = $(this).find('SC_DF_FIELD_3').text();
var description = $(this).find('SC_DF_FIELD_4').text();
var contacts = $(this).find('SC_DF_FIELD_5').text();
//clean up xml variables
url = url.substring(url.indexOf('>') + 1, url.indexOf('/a') - 1);
support_url = support_url.substring(support_url.indexOf('>') + 1, support_url.indexOf('/a') - 1); /*need to clean up contacts search later */
//alert(title + '\t' + url + '\t' + support_url + '\t' + description + '\t' + contacts);
results[num_row] = {'title:\'' + title + '\', ' + 'url:\'' + url + '\''};
//results[num_row] = title;
//results[num_row] = {text:'Link A', url:'/page1'}
num_row++
// $('<div class="items"></div>').html('<a href="' + url + '">' + title + '</a>').appendTo('#page-wrap');
});
//end of find block
prep();
}
});
});
</script>
</head>
<body>
<div id="page-wrap">
<FORM autocomplete="off"><INPUT id="searchbox" type="text"/>
</FORM></DIV>
</body>
</html>
答案 0 :(得分:3)
这会为您提供SyntaxError
,Object initializer语法无法正常工作。
如果您想在新对象中使用title
和url
变量,可以轻松实现:
//...
results[num_row] = {'title': title , 'url': url};
//...
答案 1 :(得分:1)
基本上当你写
时{'title:\'' + title + '\', ' + 'url:\'' + url + '\''}
您正在尝试设置
的值results[num_row]
等于不完整的对象
{ PropertyName }
何时需要
{ PropertyName : PropertyValue }
答案 2 :(得分:0)
尝试
results= [];
num_row = 0;
title = "myTitle";
url = "myURL";
results[num_row] = {'title': title, 'url': url}