我正在为网站进行股票交易所jQuery修复。
编辑:它根据返回的值更新网页上的ID / CLASS或输入值。
index.php:
<!doctype html> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <meta charset="utf-8"> <title>load demo</title> <script type="text/javascript"> $(document).ready(function() { $.ajax({ url: '/datacall/demo.json', dataType: 'json', success: function( resp ) { $( '#div' ).val( resp.currency[0].amount ); }, error: function( req, status, err ) { console.log( 'Something went wrong', status, err ); } }); var end = parseInt($('#value').val()); var newend = parseInt($('#div').val()); var result = $( end * newend ); $('#clickme').click(function() { $('#new').val( result ); }); }); </script> <div> <input type="hidden" value="2500" id="value" /> <input type="hidden" value="" id="div"> <input type="text" id="new" value="" readonly/> <input type="button" value="Change" id="clickme" /> </div>
目前它正在返回:
[object Object]
我也尝试过使用.text()将其返回到div
demo.json:
{ "currency" : [ { "name" : "South Africa", "code" : "ZAR", "amount" : 0.14 }, { "name" : "America", "code" : "USD", "amount" : 0.64 }, { "name" : "Europe", "code" : "GBP", "amount" : 1.29 } ] }
请有人能告诉我我做错了什么。
提前致谢!
你可以这样做:
// Create some global variables var end = parseInt($('#value').val(), 10); var newend = 0; var result = 0; $.ajax({ url: '/datacall/demo.json', dataType: 'json', success: function (resp) { // Check the values in console console.log(resp.currency[0].amount); console.log(resp.d.currency[0].amount); $('#div').val(resp.currency[0].amount); newend = parseInt(resp.currency[0].amount, 10); result = end * newend; // No need to create a new jQuery object using $() // result = $( end * newend ); }, error: function (req, status, err) { console.log('Something went wrong', status, err); } }); $('#clickme').click(function () { $('#new').val(result); });
所以这里的主要问题是:
result
asynchronous
end
newend
result = $( end * newend );