开发者

javascript: missing : after property id

I'm trying to pass my own array of objects: results[num_row] = {'title:\'' + title + '\', ' + 'url:\'' + url + '\''};

but this returns the error in firebug

when I try: results[num_row] = {title:'Link A', url:'/page1'}

it works.

Thanks,

<!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>


That gives you a SyntaxError, the Object initializer syntax doesn't work like that.

If you want to use the title and url variables in a new object, you can easily:

//...
results[num_row] = {'title': title , 'url': url};
//...


Essentially when you write

{'title:\'' + title + '\', ' + 'url:\'' + url + '\''}

You are trying to set the value of

results[num_row]

equal to an incomplete object

{ PropertyName }

when you need

{ PropertyName : PropertyValue }


Try

results= [];
num_row = 0;
title = "myTitle";
url = "myURL";
results[num_row] = {'title': title, 'url': url}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜