gpt4 book ai didi

javascript - 如何使 JQuery UI 对话框在附加新 div 时自动滚动?

转载 作者:行者123 更新时间:2023-11-28 02:07:20 27 4
gpt4 key购买 nike

我有一个显示表单的 JQuery UI 弹出对话框。通过单击“添加更多”按钮,我可以将新表单添加到 JQuery UI 对话框中。

如何使 JQuery UI 对话框在单击添加新按钮时自动滚动并向用户显示新的空白表单。

添加了div

<div>
<form id="1">
<label class="dbright">DashBoard ID:</label><span class="dbleft"><select id="dashboard_id" class="dbSettingDD"><option>-------Select-------</option><option id="#dashboard">Inventory</option><option id="#dashboard2">Quality</option><option id="#dashboard3">Complaints</option></select><br></span>

<label class="dbright">Filtering parameter:</label><span class="dbleft"><select id="filter_by"><option>-------Select-------</option></select><br></span>

<label class="dbright">Y-Axis:</label><span class="dbleft"><select id="yAxis"><option>-------Select-------</option></select><br></span>

<label class="dbright">Chart Tiltle:</label><span class="dbleft"><input type="text" id="title"><br></span>

<label class="dbright">Chart Type:</label><span class="dbleft"><input type="text" id="chart_type"><br></span>

<label class="dbright">Main Chart:</label><span class="dbleft"><input type="radio" name="mainchart" value="yes">Yes <input type="radio" name="mainchart" value="No">No<br></span>

</form>
</div>

对话框

$("#dbSetting_div").dialog({
height: 315,
width: 500,
autoOpen: false,
modal: true,
draggable: false,
buttons: {
"Save": function () {},
"Add More": function () {
l_i++;
formHtml = "<div>";
formHtml += "<form id='" + l_i + "'>";
formHtml += "<label class = 'dbright'>DashBoard ID: </label><span class = 'dbleft'><select id='dashboard_id' class='dbSettingDD'><option>-------Select-------</option>" + dropDownDashboardName + "</select></br></span>";
formHtml += "<label class = 'dbright'>Filtering parameter: </label><span class = 'dbleft'><select id='filter_by'><option>-------Select-------</option></select></br></span>";
formHtml += "<label class = 'dbright'>Y-Axis: </label><span class = 'dbleft'><select id='yAxis'><option>-------Select-------</option></select></br></span>";
formHtml += "<label class = 'dbright'>Chart Tiltle: </label><span class = 'dbleft'><input type='text' id='title'/></br></span>";
formHtml += "<label class = 'dbright'>Chart Type: </label><span class = 'dbleft'><input type='text' id='chart_type'/></br></span>";
formHtml += "<label class = 'dbright'>Main Chart: </label><span class = 'dbleft'><input type='radio' name='mainchart' value='yes'/>Yes <input type='radio' name='mainchart' value='No'/>No</br></span>";
formHtml += "</form>";
formHtml += "</div>";
$("#dbSetting_div").append("<p>" + formHtml + "</p>");
console.log(scroll);
}
}
}).css("font-size", "12px");

最佳答案

这是我的做法,尤其是添加表单的新 HTML。

首先,我会将 HTML 添加到页面,并显示 none 和某种template ID,如下所示:

<div id="formTemplate" style="display: none;">
<form id="1">
<label class="dbright">DashBoard ID:</label><span class="dbleft"><select id="dashboard_id" class="dbSettingDD"><option>-------Select-------</option><option id="#dashboard">Inventory</option><option id="#dashboard2">Quality</option><option id="#dashboard3">Complaints</option></select><br></span>
<label class="dbright">Filtering parameter:</label><span class="dbleft"><select id="filter_by"><option>-------Select-------</option></select><br></span>
<label class="dbright">Y-Axis:</label><span class="dbleft"><select id="yAxis"><option>-------Select-------</option></select><br></span>
<label class="dbright">Chart Tiltle:</label><span class="dbleft"><input type="text" id="title"><br></span>
<label class="dbright">Chart Type:</label><span class="dbleft"><input type="text" id="chart_type"><br></span>
<label class="dbright">Main Chart:</label><span class="dbleft"><input type="radio" name="mainchart" value="yes">Yes <input type="radio" name="mainchart" value="No">No<br></span>
</form>
</div>

然后,我可以在add方法中使用它来快速克隆我需要的 Material ,删除不显示,然后粘贴新表单!像这样:

$("#dbSetting_div").css("font-size", "12px").dialog({
height: 315,
width: 500,
autoOpen: false,
modal: true,
draggable: false,
buttons: {
"Save": function () { /* do work */ },
"Add More": function () {
$("#dbSetting_div").append($('<p />').append($('#formTemplate').clone().show()))
.animate({ scrollTop: $('#dbSetting_div')[0].scrollHeight}, 500);
}
}
});

其中,每当添加新表单时,我都会使用 .animate 将对话框滚动到底部。您也可以直接进行直接更改并跳过动画,例如: $("#dbSetting_div").scrollTop($('#dbSetting_div')[0].scrollHeight);

无论如何,这应该可以帮助你开始。如果您需要更多帮助,请告诉我。现在,请查看

Working Example

关于javascript - 如何使 JQuery UI 对话框在附加新 div 时自动滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17591553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com