简体   繁体   中英

populate modal form with json

I have a modal form that I created like this:

    function addeditNote(rowID,callback) {
        var ret;
        jQuery.fancybox({
            modal : true,
            overlayOpacity : 0.25,
            content : "<div class=\"contentbox modal-window modal-400\"><div class=\"boxbody\"><div class=\"boxheader clear\"><h2>Add / Edit User Notes</h2></div><br /><div class=\"box-wrap clear\"><form action=\"\" method=\"post\" class=\"form bt-space0\"><div class=\"columns clear bt-space0\"><div class=\"colText fl\"><div class=\"form-field clear\"><label for=\"textfield\" class=\"formlabel size-20 fl-space2\">Name: </label><input type=\"text\" id=\"name\" class=\"text fl-space2\" /></div><div class=\"form-field clear\"><div class=\"fl-space2\"><label for=\"textarea\" class=\"formlabel size-20\">Notes: </label></div><textarea id=\"note\" class=\"form-textarea display\" cols=\"50\" rows=\"6\" name=\"form[note]\" rel=\"textarea\"></textarea></div></div></div><div class=\"columns clear bt-space5\"></div><div class=\"form-field clear\"><input id=\"addeditNote_cancel\" class=\"button fr-space2\" type=\"button\" value=\"Cancel\"><input id=\"addeditNote_ok\" class=\"button green fr-space\" type=\"button\" value=\"Enter\"></div></form></div><!-- end of box-wrap --></div> <!-- end of box-body --></div>",



            onComplete : function() {
                jQuery("#addeditNote_cancel").click(function() {
                    ret = false;
                    jQuery.fancybox.close();
                })
                jQuery("#addeditNote_ok").click(function() {
                    ret = true;
                    jQuery.fancybox.close();
                })
            },
            onClosed : function() {
                callback.call(this,ret);
            }
        });
}

I get my json string like:

$.getJSON("GetNotes.php?id=" + rowID,
    function(data) {
        $.each(data, function(i, item) {
            $('#' + item.field).val(item.value);
                 });
});

Which returns:

{"op": "UPDATE", "id": "7","name": "Joe Public","note": "Dennis likes his coffee in the morning... He doesn't drink tea and coffee always have powdered creamer and 2 sugar's." }

The "op" just a placeholder so I know if a note exisits them I'm updating the database and if it doesn't them I'm inserting..

My problem is, I do not know whwre to put this to populate the form.. I'm still learning so was hoping someone could give me a clue how this works.. :)

Thanks! Dennis

If it doesn't work to populate the form in the same function that creates the window, try adding it as a call in the onComplete handler:

onComplete : function() {
    //All of your original code here
    $.getJSON( /* with your JSON code here */ );
},

Presumably the dialog should be ready by that time, and you can load things into the form elements as normal.

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