jquery 对话框位置受到隐藏/显示操作的严重影响
以下代码拦截对单选按钮集的单击,警告更改是否会导致数据删除,然后隐藏一个子窗体并显示另一个子窗体。所有操作都会发生,但对话框的位置会受到各个 div 的隐藏和显示的不利影响。此效果根据首先按下的单选按钮而有所不同。第一次单击“重用”按钮时,会导致对话框显示在页面之外。
如果我关闭隐藏/显示,对话框位置就可以了。
我该如何解决这个问题?
$(document).ready(function() {
$("input[name='provenance']").ready(function(){
var v=$("input[name='provenance']:checked").val();
$('div#prov_container div.optcol2').not('#'+v).hide();
$('#'+v).show();
});
// toggle hide/show of provenance field
$("input[name='provenance']").live("click", function(){
v=$(this).val();
provwarning(v); //intercept choice and check for conflicts
v=$(this).val();//may have changed due to provwarning
$('div#prov_container div.optcol2').not('#'+v).hide();//AFFECTS dialog POSITION
$('#'+v).show();//AFFECTS dialog POSITION
});
//determine if user choice will clobber existing data
//warn user
//continue or revert user choice to previous value
provwarning=function(changingto){
c= $('input[name="cross_id"]').val()? 'Cross': false;
d=$('input#del_id').val()? 'Delivery':false;
r=$('input#reuse_id').val()? 'Reuse': false;
prov_was= c||d||r;
if(!prov_was)return; //prov_was is 'Unknown', so there is no conflict
if(prov_was==changingto) return; //no change, so no worries
cw=(changingto=='Provenance')? 'unknown' : (changingto=='Delivery') ? 'delivered' : (changingto=='Reuse') ? 'reused' : 'bred onsite';
ww=(prov_was=='Provenance')? 'unknown' : (prov_was=='Delivery') ? 'delivered' : (prov_was=='Reuse') ? 'reused' : 'bred onsite';
msg="If you change the provenance to '"+cw+"' the current provenance, '"+ ww +"', will be deleted.";
m='<div id="modalpop">'+msg+'</div>';
$(m).dialog({
resizable: false,
modal: true,
title: 'Conflict with current Provenance',
buttons: {
"Continue": function() {
$(this).dialog('close');
},
"Cancel": function() {
var $radio = $('input[name="provenance"]');
$radio
.removeAttr("checked")
.filter('[value="' + prov_was + '"]')
.prop("checked", true)
.click();
$(this).dialog('close');
}
}
});
};
});
The following code intercepts a click on a radiobutton set, warns if the change will cause data deletion, and then hides a subform and shows another subform. All the actions occur, but the position of the dialog box is adversely affected by the hiding and showing of the various divs. This effect varies depending on which radiobutton is pressed first. The 'reuse' button, when clicked first, causes the dialog box to display off the page.
If I turn off the hide/show, the dialog box position is fine.
How do I fix this?
$(document).ready(function() {
$("input[name='provenance']").ready(function(){
var v=$("input[name='provenance']:checked").val();
$('div#prov_container div.optcol2').not('#'+v).hide();
$('#'+v).show();
});
// toggle hide/show of provenance field
$("input[name='provenance']").live("click", function(){
v=$(this).val();
provwarning(v); //intercept choice and check for conflicts
v=$(this).val();//may have changed due to provwarning
$('div#prov_container div.optcol2').not('#'+v).hide();//AFFECTS dialog POSITION
$('#'+v).show();//AFFECTS dialog POSITION
});
//determine if user choice will clobber existing data
//warn user
//continue or revert user choice to previous value
provwarning=function(changingto){
c= $('input[name="cross_id"]').val()? 'Cross': false;
d=$('input#del_id').val()? 'Delivery':false;
r=$('input#reuse_id').val()? 'Reuse': false;
prov_was= c||d||r;
if(!prov_was)return; //prov_was is 'Unknown', so there is no conflict
if(prov_was==changingto) return; //no change, so no worries
cw=(changingto=='Provenance')? 'unknown' : (changingto=='Delivery') ? 'delivered' : (changingto=='Reuse') ? 'reused' : 'bred onsite';
ww=(prov_was=='Provenance')? 'unknown' : (prov_was=='Delivery') ? 'delivered' : (prov_was=='Reuse') ? 'reused' : 'bred onsite';
msg="If you change the provenance to '"+cw+"' the current provenance, '"+ ww +"', will be deleted.";
m='<div id="modalpop">'+msg+'</div>';
$(m).dialog({
resizable: false,
modal: true,
title: 'Conflict with current Provenance',
buttons: {
"Continue": function() {
$(this).dialog('close');
},
"Cancel": function() {
var $radio = $('input[name="provenance"]');
$radio
.removeAttr("checked")
.filter('[value="' + prov_was + '"]')
.prop("checked", true)
.click();
$(this).dialog('close');
}
}
});
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案似乎在于事件的顺序。在原始代码中,provwarning 函数是从函数内部调用的。
provwarning
显示一个对话框。但与此同时,调用函数已经完成了隐藏/显示操作。如果用户单击“继续”,该对话框就会消失。如果用户单击“取消”,对话框将重置单选按钮集并再次触发原始功能。为了使对话框位置不受隐藏/显示操作的影响,这些操作必须在对话框关闭之后发生。我可以确保这种情况发生的唯一方法是将隐藏/显示调用放入对话框按钮功能内,如下所示:
我希望我知道为什么原始代码不等待对话框完成之前应用隐藏显示。
It appears that the answer lies in the order of events. In the original code, the
provwarning
function is called from inside a function.provwarning
displays a dialog box. But in the meantime the calling function has already done the hide/show action. If the user clicks 'Continue', the dialog box goes away. If the user clicks 'Cancel', the dialog box resets the radio button set and triggers the original function again.For the dialog position not to be affected by the hide/show actions, those actions must happen after the dialog box closes. The only way I could ensure that this happens was by putting the hide/show calls inside the dialog button functions like this:
I wish I knew why the original code doesn't wait for the dialog to complete before applying the hide-show.