jQuery追加/添加隐藏div到jQuery对话框

时间:2013-05-10 09:43:41

标签: javascript jquery html css jquery-dialog

我正在尝试将HTML文件中的div元素添加/添加到对话框(当前有一些按钮)。 div在页面加载时隐藏,CSS类为“hide”

HTML DIV:

<section>
            <div id="deliveryMethod" title="Delivery Method" class="hide">
                <p>Please select delivery method and special requirements.</p>                    
                <ul>
                    <li>
                        <label>Method:</label>
                    </li>
                    <li>
                        <div>
                            <select for="deliveryService">
                                <option value="">Please select...</option>
                                <option value="FedEx">FedEx</option>
                                <option value="UPS">UPS</option>
                            </select>
                        </div>
                    </li>
                    <li>
                        <label>Special Requirements:</label>                            
                    </li>
                    <li>
                        <textarea id="specialRequirements" type="text" value=""  maxlength="220"></textarea>
                    </li>
                </ul>
            </div>
    </section>

CSS for class = hide

.hide {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

点击按钮时的jQuery,在我调用的函数下面:

function deliveryServiceClick() {

$("#dialogDiv").dialog("open");

if ($('#dialogDiv').length == 0) {
    $('body').append("<div id='dialogDiv'><div/>");
}
var dialogDiv = $('#dialogDiv');

dialogDiv.attr("Title", "Please select your chosen delivery service.");
dialogDiv.html("<div id='deliveryMethod'><div/>");
$('body').append("<div id='deliveryMethod'><div/>");

dialogDiv.dialog({
    modal : true,
    buttons : [
            {
                text : "Process",
                class : 'large',
                click : function() {
                    //              
                }
            },
            {
                text : "Cancel",
                class : 'large',
                click : function() {
                    $(this).dialog('close');
                }
            } ]
});
}

正如你所看到的,我试图使用以下方法追加/显示我隐藏的div:

dialogDiv.html("<div id='deliveryMethod'><div/>");
$('body').append("<div id='deliveryMethod'><div/>");

我的jQuery中定义的按钮将显示在div下面。

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:3)

尝试

function deliveryServiceClick() {
    var dialogDiv = $('#dialogDiv');

    $("#dialogDiv").dialog("open");

    if (dialogDiv.length == 0) {
        dialogDiv = $("<div id='dialogDiv'><div/>").appendTo('body');
        $('#deliveryMethod').appendTo(dialogDiv).removeClass('hide')
        dialogDiv.attr("Title", "Please select your chosen delivery service.");

        dialogDiv.dialog({
            modal : true,
            buttons : [
                {
                    text : "Process",
                    class : 'large',
                    click : function() {
                        //              
                    }
                },
                {
                    text : "Cancel",
                    class : 'large',
                    click : function() {
                        $(this).dialog('close');
                    }
                } ]
        });
    }else{
        dialogDiv.dialog("open");
    }
}

演示:Fiddle