使用javascript自动完成,而不是传递变量

时间:2013-11-19 16:47:31

标签: javascript jquery

我正在使用此自动填充代码来提取股票信息:

var YAHOO = {};
YAHOO.util = {};
YAHOO.util.ScriptNodeDataSource = {};
var stockSymbol;
var compName;

    YUI({
    filter: "raw"
}).use("datasource", "autocomplete", "highlight", function (Y) {
    var oDS, acNode = Y.one("#ac-input");

    oDS = new Y.DataSource.Get({
        source: "http://d.yimg.com/aq/autoc?query=",
        generateRequestCallback: function (id) {
            YAHOO.util.ScriptNodeDataSource.callbacks =
                YUI.Env.DataSource.callbacks[id];
            return "&callback=YAHOO.util.ScriptNodeDataSource.callbacks";
        }
    });
    oDS.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "ResultSet.Result",
            resultFields: ["symbol", "name", "exch", "type", "exchDisp"]
        }
    });

    acNode.plug(Y.Plugin.AutoComplete, {
        maxResults: 10,
        resultTextLocator: "symbol",
        resultFormatter: function (query, results) {
            return Y.Array.map(results, function (result) {
                var asset = result.raw,
                    text =  asset.symbol +
                        " " + asset.name +
                        " (" + asset.type +
                        " - " + asset.exchDisp + ")";

                return Y.Highlight.all(text, query);
            });
        },
        requestTemplate:  "{query}&region=US&lang=en-US",
        source: oDS
    });

    acNode.ac.on("select", function (e) {
        alert(e.result.raw.name);
        stockSymbol = e.result.raw.symbol;
        compName = e.result.raw.name;
        alert("stockSymbol is "+stockSymbol);
        alert("compName is "+compName);
    });
});

我有一个输入文本的文本字段,然后我需要根据输入的内容显示一些信息:

$(document).ready(function(e) {
    //$(".selector").button({icons: {primary: "ui-icon-search"}});
    //$(".home").button({icons: {primary: "ui-icon-home"}});
    var q = window.location.href.split("?ac-input=");
    alert("q is "+q)
    if (q && q[1])
        $("input").val(q[1]);
        alert("what is this?" +q[1]);
    runForm();
});

..........

function runForm(){
    $("#stock_news").html("");
    //var stockSymbol = e.result.raw.symbol;
    alert('stockSymbol in runForm is ' +stockSymbol);
    alert('compname is runForm is '+compName);

    var newsStocks = "http://query.yahooapis.com/v1/public/yql?.......";

    $.getJSON(newsStocks, function(data) {
        var headlines = data.query.results.a;
        newStr = "<h3>Current Headlines</h3>";
        newStr += "<ol>";
        for(var i=0; i<headlines.length; ++i){
            var news = headlines[i];        
                newStr += "<li><a href='"+news.href+"'>"+news.content+"</a></li>";
            }
            newStr += "</ol>";
            $("#stock_news").html(newStr);
        });

        htmlStr = '<img src="http://chart.finance.yahoo.com/z?s='+stockSymbol+'&t=5d&q=l&l=off&z=s&region=US&lang=en-EN"/>';
    $("#stock_graph").html(htmlStr);

}

正在发生的事情是,stockSymbolcompName在输入时设置正确,但由于某些原因,页面上没有任何渲染,因为q未定义....任何关于我在哪里的想法弄乱了吗?

想出来。我需要在runForm

内拨打acNode.ac.on("select", function (e)

0 个答案:

没有答案