使用Yahoo Finance检索库存更新 [英] Retrieving stock updates using Yahoo Finance

查看:124
本文介绍了使用Yahoo Finance检索库存更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试获取特定公司的股票报价.在我的代码中,我在文本框(symb)中给出了特定公司的符号(例如:FB ),当我单击按钮(getupdate)时, >它应列出有关特定股票的详细信息,我将在文本框中输入该详细信息.

这是我的代码:

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function($){
$('getupdate').click(function() {

        var symbol = $('input[id=symb]').val(); \\For Example:FB

        var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'+symbol+'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

        $.getJSON(url, function(data) {
            var items = [];
            $.each(data.query.results.quote, function(key, val) {
                items.push('<li id="' + key + '">' + val + '</li>');

            });
            $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('body');
        });
});
});
</script>

</head>
<body>
<div style="padding:16px;">
    Stock Ticker : <input id="symb" type="textbox" value="Ticker"></input>
</div>
<button id="getupdate" name = "getupdate" type="button">Get Updates!</button>
</body>
</html>

我要去哪里错了?

谢谢.

解决方案

您的JS代码有几个错误:

  1. 按钮选择器不正确:$('getupdate') => $('#getupdate');
  2. url值内的引号错误;
  3. Yahoo API的查询字符串错误;
  4. 注释符号\\For Example:FB错误.

您的JS应该是这样的:

jQuery(document).ready(function($){
    $('#getupdate').click(function() {
            var symbol = $('input[id=symb]').val(); 
            var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22' + symbol + '%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
            $.getJSON(url, function(data) {
                var items = [];
                $('#results').html('');
                $.each(data.query.results.quote, function(key, val) {
                    items.push('<li id="' + key + '">' + val + '</li>');

                });
                $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('#results');
            });
    });
});

并且请在HTML的<button>标记之后添加此代码.这将帮助您在进行新查询之前轻松清除结果:

<div id="results"></div>

这里是一个示例: http://jsfiddle.net/6EFqk/1/

我不确定输出格式是否正确,请根据需要重新格式化.

Basically what i am trying do is retrieving stock quotes for specific company . In my code I am giving symbol of specific company(eg:FB) in a textbox(symb) and when i click the button(getupdate) it should list out details regarding that specific stock which i enter the text box .

Here's my code :

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function($){
$('getupdate').click(function() {

        var symbol = $('input[id=symb]').val(); \\For Example:FB

        var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'+symbol+'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

        $.getJSON(url, function(data) {
            var items = [];
            $.each(data.query.results.quote, function(key, val) {
                items.push('<li id="' + key + '">' + val + '</li>');

            });
            $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('body');
        });
});
});
</script>

</head>
<body>
<div style="padding:16px;">
    Stock Ticker : <input id="symb" type="textbox" value="Ticker"></input>
</div>
<button id="getupdate" name = "getupdate" type="button">Get Updates!</button>
</body>
</html>

Where am i going wrong ?

Thanks in advance.

解决方案

You have several mistakes in your JS code:

  1. Incorrect selector for a button: $('getupdate') => $('#getupdate');
  2. Wrong quotes inside url value;
  3. Wrong query string to Yahoo API;
  4. Wrong comment sign \\For Example:FB.

Your JS should be like this:

jQuery(document).ready(function($){
    $('#getupdate').click(function() {
            var symbol = $('input[id=symb]').val(); 
            var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22' + symbol + '%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
            $.getJSON(url, function(data) {
                var items = [];
                $('#results').html('');
                $.each(data.query.results.quote, function(key, val) {
                    items.push('<li id="' + key + '">' + val + '</li>');

                });
                $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('#results');
            });
    });
});

And please add this code after <button> tag in your HTML. This will help you easily clear results before new query:

<div id="results"></div>

Here is an example: http://jsfiddle.net/6EFqk/1/

I'm not shure that ouput format is correct, please reformat it as you like.

这篇关于使用Yahoo Finance检索库存更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆