简体   繁体   English

使用JQuery在动态JQuery UI对话框中设置动态创建的表单元素的值

[英]Set value for dynamically created form elements in dynamic JQuery UI dialog using JQuery

I'm trying to read data from a service (returns JSON object) and create an editable form in a dynamic JQuery UI dialog so the end user can use it to make changes and submit. 我正在尝试从服务中读取数据(返回JSON对象)并在动态JQuery UI对话框中创建可编辑的表单,以便最终用户可以使用它来进行更改和提交。 The trouble is, when I get data from the server, I can't seem to set the data in the form. 问题是,当我从服务器获取数据时,我似乎无法在表单中设置数据。 If I don't use a dialog, then everything works. 如果我不使用对话框,那么一切正常。

I created an associated JSFiddle in case it helps. 我创建了一个关联的JSFiddle ,以防它有所帮助。

var dialog_box = $('<div></div>');
var animal = { kind : "Cat", has_whiskers : true };
var s = $('<select />', {
    "id":"s1"
}).append(
    $('<option />', 
        {
            value:"Dog", 
            text:"Dog"
        }
    ),
    $('<option />', 
        {
            value:"Cat", 
            text:"Cat"
        }
    ),
    $('<option />', 
        {
            value:"Bird", 
            text:"Bird"
        }
    )
);
s.appendTo(dialog_box);

// doesn't work
$('#s1 option:[value="'+ animal.kind +'"]').prop('selected', true);

var new_div = $('<div/>').html('<input type="checkbox" id="has_whiskers_checkbox" />');

new_div.appendTo(dialog_box);

(animal.has_whiskers) ? $("#has_whiskers_checkbox").prop("checked", true) : $("#has_whiskers_checkbox").prop("checked", false);

dialog_box.dialog({
     autoOpen: false,
     modal: true,
     buttons: {
         "OK": function() {
              console.log("OK Pressed");
              $( this ).dialog( "close" );
              $( this ).remove();
           }
        }
}).dialog('open');
$('#s1 option:[value="'+ animal.kind +'"]', dialog_box).prop('selected', true);

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

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