我想出了如何展示“你确定要离开这个页面吗?”'用户离开网页时的确认对话框。有用。现在我需要弄清楚如果用户在页面的输入字段中输入了某些内容,如何触发此对话框。
以下代码有效。但每当用户试图离开页面时它就会触发:
确认侦听器功能:
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = 'Are you sure you want to leave this page?'
+ 'We hope you had a great experience!';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
HTML:
<input id="storyBox" type="text" />
我需要检查用户是否输入任何文本到输入字段并决定是否触发请假确认对话框:
if(document.getElementById("storyBox").value.length != 0){
//Trigger the Event Listener
}
我的问题是,我在哪里放置这个条件?显然围绕EventListener的条件不会起作用。如果输入字段不为空
,我该如何触发此事件?答案 0 :(得分:3)
如果你没有从eventListener内部返回,弹出窗口将不会显示,所以你可以这样做:
window.addEventListener("beforeunload", function (e) {
if(document.getElementById("storyBox").value.length != 0){
var confirmationMessage = 'Are you sure you want to leave this page?'
+ 'We hope you had a great experience!';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
}
});
(点击“运行”按钮时会出现beforeunload
。所以你可以测试一下。)
答案 1 :(得分:1)
这段代码很简单,但可以正常使用
import psycopg2 as pg
import pandas.io.sql as psql
connection = pg.connect("dbname=BeaconDB user=admin password=root")
dataframe = psql.read_sql_query(sql="SELECT * from encounters2", con=connection, index_col='encounter_id')
print('size::::', dataframe.size)
答案 2 :(得分:1)
var confirmMessage = function (e) {
var confirmationMessage = 'Are you sure you want to leave this page?'
+ 'We hope you had a great experience!';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
};
function bindEvent(element){
if(document.getElementById("storyBox").value.length != 0){
window.addEventListener("beforeunload", confirmMessage);
}
else {
window.removeEventListener("beforeunload", confirmMessage);
}
};
这也有效。当输入中没有文本时,这将删除eventListener。并添加eventListner文本框中有文本