Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

jquery - "Walk" JSON response and populate form fields -- more efficient approach?

I know there is a far more elegant/efficient way of doing this (in php I would use foreach) but with jQuery how can I walk the var/val pairs of a JSON response and populate form fields with the same id's as the field names in the JSON response?

Here is my JSON response:

[{"field":"svendor_name","value":"Vendor Name Inc."},{"field":"svendor_addr1","value":"1234 Vendor Lane."},{"field":"svendor_addr2","value":"Suite 100"},{"field":"svendor_city"
,"value":"Vendorville"},{"field":"svendor_state","value":"CA"},{"field":"svendor_zip","value":"90210"},{"field"
:"svendor_phone","value":"800-555-1234"}]

Here is my jQuery code for populating the form:

$(document).ready(function()
{
    $('#svendor_name').bind("change", function()
    {
        var svendor = $("#svendor_name").val();
        svendor = svendor.replace(/&/g, '*');
        $.getJSON("get_vendors.php?sname=" + svendor,
        function(data)
        {
            $.each(data,
                function(i, item)
                {
                    if(item.field == "svendor_name")
                    {
                        $("#svendor_name").val(item.value);
                    }
                    else if(item.field == "svendor_addr1")
                    {
                        $("#svendor_addr1").val(item.value);
                    }
                    else if(item.field == "svendor_addr2")
                    {
                        $("#svendor_addr2").val(item.value);
                    }
                    else if(item.field == "svendor_city")
                    {
                        $("#svendor_city").val(item.value);
                    }
                    else if(item.field == "svendor_state")
                    {
                        $("#svendor_state").val(item.value);
                    }
                    else if(item.field == "svendor_zip")
                    {
                        $("#svendor_zip").val(item.value);
                    }
                    else if(item.field == "svendor_phone")
                    {
                        $("#svendor_phone").val(item.value);
                    }
                    else if(item.field == "svendor_id")
                    {
                        $("#svendor_id").val(item.value);
                    }
            });
        });
    });
});

That all works fine and good but I really want to avoid all the if/else statements and just use the data coming back from the getJSON method to determine what fields get populated with what values. What is a cleaner/more effective approach to this?

-- Nicholas

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get rid of all "if" statements by replacing your $.each with this:

$.each(data, function(i, item){
  $("#"+item.field).val(item.value);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...