在我的项目中,我使用jquery ui对话框。
在transmit_div关闭之前,staff_div有John-Stack,sltStfId有Robert-Sansa。在transmit_div关闭后,我想删除内容 “John-Stack”和“Robert-Sansa”。所以我在初始化transmit_div对话框时选择了beforeclose参数。
这是js代码:
<script>
$(document).ready(function()
{
$('#transmit_app').click(function(){
$('#transmit_div').dialog("open");
$('#transmit_div').dialog("option","title");
});
});
$('#transmit_div').dialog({
autoOpen:false,
title:"selectUser",
modal:true,
height:500,
width:600,
show:{effect:"blind",duration: 1000},
hide:{effect:"explode",duration: 1000},
beforeclose: function() {
staff_div.innerHTML="<input type='button' id='selAllButton' value='selectAll'><br />";
sltStfId.innerHTML="";
}
});
</script>
这是html代码:
<input type="button" id="transmit_app" value="trsmit" />
<div id="transmit_div" >
<div id="staff_div" class="staff" >
<input type="button" id="selAllButton" value="selectAll"><br />
John-Stack
</div>
<div class="selected_staff" >
<input type="button" onclick="delete_allStaff()" value="deleteAll">
<div id="sltStfId">
Robert-Sansa
</div>
</div>
</div>
但不幸的是,它失败了。我收集transmit_div对话框,然后再次打开它。 “John-Stack”和“Robert-Sansa”仍然存在。
谁能帮帮我?
答案 0 :(得分:2)
你可能想要这个代码。希望它有所帮助。
<script>
$(document).ready(function(){
$('#transmit_app').click(function(){
$('#transmit_div').dialog("open");
$('#transmit_div').dialog("option","title");
});
$('#transmit_div').dialog({
autoOpen:false,
title:"selectUser",
modal:true,
height:500,
width:600,
show:{effect:"blind",duration: 1000},
hide:{effect:"explode",duration: 1000},
beforeClose: function myCloseDialog() {
$('#staff_div').html("<input type='button' id='selAllButton' value='selectAll'><br />");
$('#sltStfId').html("");
}
});
});
</script>
答案 1 :(得分:0)
$(document).ready(function()
{
$('#transmit_app').click(function(){
$('#transmit_div').dialog("open");
$('#transmit_div').dialog("option","title");
});
$("#delete").click(function(){
$("#sltStfId,#staff_div").remove();
})
});
$('#transmit_div').dialog({
autoOpen:false,
title:"selectUser",
modal:true,
height:500,
width:600,
show:{effect:"blind",duration: 1000},
hide:{effect:"explode",duration: 1000},
beforeclose: function() {
staff_div.innerHTML="<input type='button' id='selAllButton' value='selectAll'><br />";
sltStfId.innerHTML="";
}
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="button" id="transmit_app" value="trsmit" />
<div id="transmit_div" >
<input type="button" id="selAllButton" value="selectAll"><br />
<div id="staff_div" class="staff" >
John-Stack
</div>
<div class="selected_staff" >
<input type="button" value="deleteAll" id="delete">
<div id="sltStfId">
Robert-Sansa
</div>
</div>
</div>
答案 2 :(得分:0)
You want to use the close
event function, not the beforeclose
event.
Also you want to target the container div to empty()
all elements contained in it.
$("#transmit_div").empty();
$(document).ready(function() {
$('#transmit_app').click(function() {
$('#transmit_div').dialog("open");
$('#transmit_div').dialog("option", "title");
});
});
$('#transmit_div').dialog({
autoOpen: false,
title: "selectUser",
modal: true,
height: 500,
width: 600,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
},
close: function()
{
deleteAll();
}
});
$("#delete-all").on("click", function()
{
deleteAll();
});
function deleteAll()
{
$("#transmit_div").empty();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
<input type="button" id="transmit_app" value="trsmit" />
<div id="transmit_div">
<div id="staff_div" class="staff">
<input type="button" id="selAllButton" value="selectAll"><br /> John-Stack
</div>
<div class="selected_staff">
<input id="delete-all" type="button" value="deleteAll">
<div id="sltStfId">
Robert-Sansa
</div>
</div>
</div>