最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Put json return value from ajax in one or more text box - Stack Overflow

programmeradmin2浏览0评论
$('select[name=\'service_id\']').bind('change', function() {

    service_id = $('select[name=\'service_id\']').val();

    $.ajax({

        url: 'myajaxfile.php&service_id=' +  service_id,
        dataType : 'json',
        success: function(json) {

            var max_use = json.max_use;
            var used = json.used;
            var amount = json.amount;

            $('input[name=\'max_use\']').val(max_use);
            $('input[name=\'used\']').val(amount);

        }
    });
});

Above is my piece of code. All I want is to bind the value from json result which is ing fine to two or more text boxes which is not happening. The json result is like [{"max_use":"0","period":"30","amount":"99"}] which is very correct. On doing an alert to amount it says undefined. Would be great help if point me out what's the problem is? I tried searching on stackoverflow but found no perfect solution that works. Thanks.

$('select[name=\'service_id\']').bind('change', function() {

    service_id = $('select[name=\'service_id\']').val();

    $.ajax({

        url: 'myajaxfile.php&service_id=' +  service_id,
        dataType : 'json',
        success: function(json) {

            var max_use = json.max_use;
            var used = json.used;
            var amount = json.amount;

            $('input[name=\'max_use\']').val(max_use);
            $('input[name=\'used\']').val(amount);

        }
    });
});

Above is my piece of code. All I want is to bind the value from json result which is ing fine to two or more text boxes which is not happening. The json result is like [{"max_use":"0","period":"30","amount":"99"}] which is very correct. On doing an alert to amount it says undefined. Would be great help if point me out what's the problem is? I tried searching on stackoverflow but found no perfect solution that works. Thanks.

Share Improve this question edited Apr 23, 2015 at 9:03 Alessandro Da Rugna 4,70520 gold badges43 silver badges66 bronze badges asked Apr 23, 2015 at 8:59 Preeti MauryaPreeti Maurya 4311 gold badge7 silver badges18 bronze badges 1
  • would you please help me for that please? – Preeti Maurya Commented Apr 23, 2015 at 9:04
Add a ment  | 

3 Answers 3

Reset to default 4

try

var t = JSON.parse(json);

 var max_use = t[0]["max_use"];
 var used = t[0]["used"];
 var amount = t[0]["amount"];

Your json is an array with an object in it. So you must either change json, or access object properties like that:

var max_use = json[0].max_use;
var used = json[0].used;
var amount = json[0].amount;

proper json in your case would look like that:

{"max_use":"0","period":"30","amount":"99"}

without []

result is an array, you have value at 0 index, so use as below

result  = [{"max_use":"0","period":"30","amount":"99"}][0]

fetch as below

result.max_user
result.period
result.amount

or

result["max_user"]
result["period"]
result["amount"]
发布评论

评论列表(0)

  1. 暂无评论