0

我在提交的表单中有一个对话框 div。div 由使用 JavaScript 动态填充的复选框填充。当我单击链接打开对话框时,选择几个复选框,关闭,提交,我的 PHP 进程页面没有将选定的复选框注册为 POST 项。

所以,我相信因为复选框是在页面加载后动态生成的,它们实际上并没有注册为表单中的元素?这个对吗?有了这些知识(或缺乏知识),我不知道如何规避这一点。

任何帮助将不胜感激,我四处寻找帮助无济于事。

HTML:

<form action="submit.php" method="POST">

        <div id="dialog" style="font-size:14px;">
            <div id="states_container">
                <div style="width:33%; float:left;" id="col1">

                </div>

                <div style="width:33%; float: left;" id="col2">

                </div>

                <div style="width:33%; float: left;" id="col3">

                </div>
            </div>

        </div>
   <input type="submit" value="Submit">
</form>

submit.js(是的,我知道这是糟糕的代码,但我要重构):

$(function() {
    $( "#dialog-link" ).click(function( event ) {
        $( "#dialog" ).dialog( "open" );
        event.preventDefault();
    });

    $( "#dialog" ).dialog({
    autoOpen: false,
    width: 600,
    buttons: [
        {
            text: "Ok",
            click: function() {
                $( this ).dialog( "close" );
            }
        },
        {
            text: "Cancel",
            click: function() {
                $( this ).dialog( "close" );
            }
        }
    ]
    });

    var states1 = {
        AL: "Alabama", 
        AK: "Alaska", 
        AZ: "Arizona", 
        AR: "Arkansas", 
        CA: "California", 
        CO: "Colorado", 
        CT: "Connecticut", 
        DE: "Delaware", 
        DC: "District Of Columbia", 
        FL: "Florida", 
        GA: "Georgia", 
        HI: "Hawaii", 
        ID: "Idaho", 
        IL: "Illinois", 
        IN: "Indiana", 
        IA: "Iowa",
        KS: "Kansas"
    };

    var states2 = {

        KY: "Kentucky", 
        LA: "Louisiana", 
        ME: "Maine", 
        MD: "Maryland", 
        MA: "Massachusetts", 
        MI: "Michigan", 
        MN: "Minnesota", 
        MS: "Mississippi", 
        MO: "Missouri", 
        MT: "Montana",
        NE: "Nebraska",
        NV: "Nevada",
        NH: "New Hampshire",
        NJ: "New Jersey",
        NM: "New Mexico",
        NY: "New York",
        NC: "North Carolina"
    };

    var states3 = {
        ND: "North Dakota",
        OH: "Ohio", 
        OK: "Oklahoma", 
        OR: "Oregon", 
        PA: "Pennsylvania", 
        RI: "Rhode Island", 
        SC: "South Carolina", 
        SD: "South Dakota",
        TN: "Tennessee", 
        TX: "Texas", 
        UT: "Utah", 
        VT: "Vermont", 
        VA: "Virginia", 
        WA: "Washington", 
        WV: "West Virginia", 
        WI: "Wisconsin", 
        WY: "Wyoming"
    };



    $.each(states1, function(key, value) {
        $('#col1').append(
            '<label for="' + value + '"><input type="checkbox" name="'+value+'" id="'+value+'">'+value+'</label><br/>'
        );
    });


    $.each(states2, function(key, value) {
        $('#col2').append(
            '<label for="' + value + '"><input type="checkbox" name="'+value+'" id="'+value+'">'+value+'</label><br/>'
        );
    });


    $.each(states3, function(key, value) {
        $('#col3').append(
            '<label for="' + value + '"><input type="checkbox" name="'+value+'" id="'+value+'">'+value+'</label><br/>'
        );
    });
});
4

1 回答 1

0

把它放在表单标签中

<div id="dvDialog"></div>

并在打开表单时将其放置在 jQuery 中,然后再打开对话框命令

$('#dialog').parent().appendTo('#dvDialog');

我们需要这样做是因为,当打开对话框时,它会附加在 DOM 中而不是 Form 标签中,并且我们知道,当我们应该将数据发布到任何页面时,我们需要在 form 标签中进行控件或输入,所以这样我们在 div 中添加对话框,放置在表单标签内 DOM 中的表单标签下方。

于 2013-09-20T14:14:28.210 回答