Javascript 每次刷新时都会显示一个对话框
我必须在我的入门网站上建立一个对话框。问题是,它每次都会出现,我无法关闭它(单击按钮只是刷新站点,对话框再次出现)。我也知道原因,但我能解决它 这是我的对话Javascript 每次刷新时都会显示一个对话框,javascript,web,cookies,primefaces,dialog,Javascript,Web,Cookies,Primefaces,Dialog,我必须在我的入门网站上建立一个对话框。问题是,它每次都会出现,我无法关闭它(单击按钮只是刷新站点,对话框再次出现)。我也知道原因,但我能解决它 这是我的对话 <p:dialog id="ac-wrapper" widgetVar="test" style='display: none; background:white;' modal="true" resizable="false" closeOnEscape="true" closable="true"
<p:dialog id="ac-wrapper" widgetVar="test" style='display: none; background:white;' modal="true"
resizable="false" closeOnEscape="true" closable="true" visible="true">
<div id="popup">
<h2>Some Content</h2>
<input type="submit" name="submit" value="Submit"
onclick="DialogBox('hide')" />
</div>
</p:dialog>
一些内容
以下是应处理此问题的javascript:
<script type="text/javascript">
$ = jQuery;
function DialogBox(hideOrshow) {
if (hideOrshow == 'hide') {
localStorage.setItem("isShown",1);
document.getElementById('ac-wrapper').style.display = "none";
document.getElementById('ac-wrapper').visible="false";
$("#ac-wrapper").close();
}
else if(localStorage.getItem("isShown") == null) {
document.getElementById('ac-wrapper').removeAttribute('style');
localStorage.setItem("isShown",1);
}
}
window.onload = function () {
setTimeout(function () {
if(localStorage.getItem("isShown") != 1 ){
DialogBox('show');
}
else if(localStorage.getItem("isShown")){
$("#ac-wrapper").remove();
}}, 1000);
}
</script>
$=jQuery;
函数对话框(隐藏显示){
if(hideOrshow=='hide'){
setItem(“isShown”,1);
document.getElementById('ac-wrapper').style.display=“无”;
document.getElementById('ac-wrapper').visible=“false”;
$(“#ac包装”).close();
}
else if(localStorage.getItem(“isShown”)==null){
document.getElementById('ac-wrapper')。removeAttribute('style');
setItem(“isShown”,1);
}
}
window.onload=函数(){
setTimeout(函数(){
if(localStorage.getItem(“isShown”)!=1){
对话框(“显示”);
}
else if(localStorage.getItem(“isShown”)){
$(“#ac包装”).remove();
}}, 1000);
}
通过渲染站点,对话框始终出现,因为visible属性设置为“true”。我想顺序不对。它应该首先检查本地存储,然后渲染元素,我无法让它正常工作。我也在这里寻找类似问题的答案,但没有任何帮助。问题在于此
<input type="submit" name="submit" value="Submit"
onclick="DialogBox('hide')" />
因为它是一个输入类型submit,所以默认为表单行为。。。也就是使用GET重定向到同一url。将类型和值更改为“button”和“close”,您的问题将得到解决不要调用任何submit,不要使用submit作为按钮,更改为
type=“button”value=“close”
还有为什么DOM和jQuery混合使用???如果您有这样的东西,请使用jQuery。我可能有你想要的相反效果,因为你选择isShown vs show函数对话框(hideOrshow){var show=hideOrshow!='hide';//更有意义的是localStorage.setItem(“isShown”,show);$('#ac wrapper')。切换(show)if(!show)$(“#ac wrapper”).close()}$(函数(){setTimeout(函数(){var show=localStorage.getItem(“isShown”)==1;if(show){DialogBox('show');}else$(“#ac wrapper”).remove();},1000)}
如果注释解决了您的问题,请随意删除该问题您有一个visible=“true”
所以每次我在问题下对此发表评论时都会显示出来是很正常的。顺便说一句,表单的默认行为是被编辑的,谢谢。如果你想重新发布你的评论作为答案,我很乐意删除我的评论。没关系。我更喜欢删除这个问题-这对其他人来说不是非常有用