如何将jquery数组添加到html表单中,以将复选框动态添加到对话框中

How to add jquery array into html form to add checkboxes dynamically into dialogue box

本文关键字:添加 复选框 对话框 动态 html jquery 数组 表单      更新时间:2023-09-26

>我有一个jQuery数组,其中包含用于生成复选框的HTML代码

<input type="checkbox"   id="Mumbai" name="Mumbai" value="Mumbai" />Mumbai<br />,
<input type="checkbox"   id=" Delhi" name=" Delhi" value=" Delhi" /> Delhi<br />,
<input type="checkbox"   id=" Bangalore" name=" Bangalore" value=" Bangalore" />Bangalore<br />

现在,根据我的需要,我想将这个名为输出的数组添加到包含在html形式的jquery对话框中。

这是我的代码。

var $dialog = $('<div></div>')
        .html('<form id="myform" action="">output</form>')
        .dialog({
            autoOpen: false,
            title: 'Select Sites',
            buttons: {
                "Submit": function() {  $('form#myform').submit();},
                "Cancel": function() {$(this).dialog("close");}
            }
        });

我必须以.html形式添加名为 output 的数组,我在代码中像这样添加,但它没有显示复选,而是在警报框消息中显示输出..

任何帮助将不胜感激。提前谢谢..

您必须将 html 分成两个不同的部分。例如

.html('<form id="myform" action="">' + output + '</form>')

试试这个,

var $dialog = $('<div></div>')
    .html('<form id="myform" action="">'+$('input:checkbox').clone()+'</form>')
    .....

试试这个

$(window).load(function() {
        $.ajax({
            type: 'GET',
            url: 'Sites',
            success: function(data) {
                var city=data.city
                for(var i in city)
                {
                   var output='<input type="checkbox"   id="'+city[i]+'" name="'+city[i]+'" value="'+city[i]+'" />'+city[i]+'<br />'
                }
                consoloe.log(output)
            }
        });
    });

请参阅此"如何让 jQueryUI 动态加载内容"对话框