简体   繁体   中英

how to auto fill value with jquery from json object

i have some problem, this is my case, i have two input tag tag input one

<input type="text" name="samaccountname" id="samaccountname" />

tag input two

<input type="text" name="question" id="question" readonly />

and json object such as :

[{"samaccountname":"19000","question":"Your Birthday"},{"samaccountname":"19100","question":"Your Car Brand"}];

how to auto fill the no 2 tag input (question) if no 1 tag input (samaccountname) is same value with my json object, with jquery with $.each?

Try like this-

$(document).ready(function(){

    var mJson = [{"samaccountname":"19000","question":"Your Birthday"},{"samaccountname":"19100","question":"Your Car Brand"}];

    $("#samaccountname").keyup(function(){
        var id = $(this).val();
        $.each(mJson, function(i,v){
            if(id == v.samaccountname){
                $("#question").val(v.question);
                return false; // breaks the loop
            }
        });
    });

});
$(document).ready(function(){
    var uri = urlListUser('listuser',0,-10);
    $("#samaccountname").keyup(function(){
        var id = $(this).val();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: uri,
            data:$(this).serialize(),
            success: function(data){
                $.each(data, function(i,v){
                    if(id == v.nik){
                        $("#question").val(v.question);
                    }
                    console.log(v.question);
                });
            } 
        });    
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM