简体   繁体   English

用JSON填充模式形式

[英]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: 我得到我的json字符串,如:

$.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.. “ op”只是一个占位符,所以我知道注释是否存在,我正在更新数据库,如果不是,我正在插入..

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处理程序中的调用:

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. 大概到那时对话框应该已经准备好了,您可以像往常一样将内容加载到表单元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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