簡體   English   中英

json讀取功能中的javascript設置變量

[英]javascript set variable in a json read function

我正在使用javascript從url json API中讀取一些值。 我有一個組合選擇,當我選擇一個選項時,我在變量中設置了值,然后嘗試將其設置為我的json讀取函數,也就是說,我想從json中讀取值。 但是它不檢索數據,僅打印變量。 連鎖店有這方面

data.clp.name

我想為任何選擇組合框更改單詞“ clp”。

這是我的代碼:

<!DOCTYPE html>
<html>

<head>
 <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
   </head>

<body>

<select id="myselect" onchange="test(this)">
<option value="mxn">Mexico</option>
<option value="eur">España</option>
<option value="cop">Colombia</option>
<option value="clp">Chile</option>
<option value="pen">Peru</option>
</select>

<p id="demo1"></p>


<script>

function test(selectObject){
    var v =   $( "#myselect" ).val();
    var valor = selectObject.value;


    var getJSON1 = function (url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'json';
        xhr.onload = function () {
            var status = xhr.status;
            if (status === 200) {
                callback(null, xhr.response);
            } else {
                callback(status, xhr.response);
            }
        };
        xhr.send();
    };

    getJSON1('http://www.floatrates.com/daily/usd.json',
        function (err, data) {
            if (err !== null) {
                alert('Something went wrong: ' + err);
            } else {
                //alert('Your query count: ' + data.moneda.dolar);
                //$("div").append(data.query.lang + " ");
                var t = `data.${valor}.code`

                var text = `Etiqueta: ${t}<br>
                Nombre : ${data.cop.name}<br>
                Dolar : ${data.cop.rate}`                

               // document.getElementById("demo1").innerHTML = data.valor.name;
                $("#demo1").html(text);
            }
        });    
}
    </script>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</body>

</html>

謝謝!

您需要使用valor的值訪問數據對象,如下所示:data [valor]。 請參閱下面的解決方案:

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" /> </head> <body> <select id="myselect" onchange="test(this)"> <option value="mxn">Mexico</option> <option value="eur">España</option> <option value="cop">Colombia</option> <option value="clp">Chile</option> <option value="pen">Peru</option> </select> <p id="demo1"></p> <script> function test(selectObject){ var v = $( "#myselect" ).val(); var valor = selectObject.value; var getJSON1 = function (url, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'json'; xhr.onload = function () { var status = xhr.status; if (status === 200) { callback(null, xhr.response); } else { callback(status, xhr.response); } }; xhr.send(); }; getJSON1('https://www.floatrates.com/daily/usd.json', function (err, data) { if (err !== null) { alert('Something went wrong: ' + err); } else { //alert('Your query count: ' + data.moneda.dolar); //$("div").append(data.query.lang + " "); var t = data[valor].code var text = `Etiqueta: ${t}<br> Nombre : ${data[valor].name}<br> Dolar : ${data[valor].rate}` // document.getElementById("demo1").innerHTML = data.valor.name; $("#demo1").html(text); } }); } </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM