【问题标题】:Taking the input field from a JSON从 JSON 中获取输入字段
【发布时间】:2021-04-28 00:20:12
【问题描述】:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test AJAX</title>
    </head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
    function send()
    {
    var index = $('#index').val();
     $(document).ready(function() {
           
            $("#submit").click(function(event){
               $.getJSON('bursa.json', function(jd) {
                  $('#Reserved').html('<p>' + jd.response[0].index + '</p>');
               });
            });
               
         });
    }
    </script>
    
    <body>
    <form method="get">
    Select <input type="text" id="index"><br>
    <input type="button" id="submit" onclick="send()" value="Submit">
    </form>
    <div id="Reserved" style="border:1px solid red; height: 300px; width:400px"></div>
    </body>
</html>

我是 Jquery 的初学者,我一直在尝试从我为测试创建的一些 JSON 文件中获取一些数据。基本上我想要的是能够允许用户从 json 文档中选择他想要的字段。我的 json 文件是这样的

"response": [
        {
            "c": "205.84",
            "h": "206.70",
            "l": "204.27",
            "ch": "-1.57",
            "cp": "-0.76%",
            "t": "1611349199",
            "s": "BA",
            "cty": "united-states",
            "ccy": "USD",
            "exch": "NYSE",
            "id": "1",
            "tm": "2021-01-22 20:59:59"
        },

例如,如果我尝试打印 jd.response[0].cty,它会正确显示“美国”,但正如我所说,我想要的是允许用户选择他自己的字段。到目前为止,它只显示“未定义”。提前感谢您的帮助。

【问题讨论】:

  • 函数内部不需要document.ready
  • 而不是.index,使用[index]
  • 嘿,我发布了一个答案,如果你接受并投票,那就太好了!
  • 6 分钟 cd,抱歉,我还是新手,很快 :)

标签: html jquery json ajax


【解决方案1】:

不要使用.index,而是使用[index]

$('#Reserved').html('<p>' + jd.response[0][index] + '</p>');

也称为bracket notation

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多