我目前有一个表格,如果表格未填写,我该如何使其无法执行javascript?就像这些没有标题一样,我会收到一条警告,提示“请填写表格”。因此,我会提醒用户填写表单,然后才能添加通知。
到目前为止,这里有我的代码。
let note = {
id: 0,
title: "",
text: "",
trigger: {
at: ""
},
badge: 0,
data: {
prop: "",
num: 0
}
}
let day = "";
let month = "";
let year = "";
let hour = "";
let minute = "";
let date = "";
document.getElementById("add-btn").onclick = () => {
let id = new Date().getTime();
let triggerDate = new Date(year, month, day, hour, minute);
let noteOptions = {
id: id,
title: note.title || "Default title",
text: note.text || "Default text",
at: triggerDate,
badge: 1,
data: {
prop: "prop value",
num: 42
}
};
cordova.plugins.notification.local.schedule(noteOptions);
localStorage.setItem(id, JSON.stringify(noteOptions));
navigator.notification.alert("Added notification id " + id + " will notify at: " + triggerDate.toString());
window.open("index.html");
}
document.getElementById("title").addEventListener("change", (event) => {
note.title = event.target.value;
});
document.getElementById("dateinput").addEventListener("blur", (event) => {
year = "", month = "", day = "";
date = new Date(event.target.value);
year = date.getFullYear();
month = date.getMonth();
day = date.getDate() + 1;
});
document.getElementById("timeinput").addEventListener("change", (event) => {
let time = event.target.value;
if(time) {
hour = (time.split(":")[0]);
minute = (time.split(":")[1]);
}
});
document.getElementById("text").addEventListener("change", (event) => {
note.text = event.target.value;
});
答案 0 :(得分:0)
您可以检查是否已设置note.title和note.text。如果您需要更多警报,可以继续放置更多else if (condition)
。
document.getElementById("add-btn").onclick = () => {
if (!note.title) {
navigator.notification.alert('Missing title.');
}
else if (!note.text) {
navigator.notification.alert('Missing text.');
}
else {
let id = new Date().getTime();
let triggerDate = new Date(year, month, day, hour, minute);
let noteOptions = {
id: id,
title: note.title || "Default title",
text: note.text || "Default text",
at: triggerDate,
badge: 1,
data: {
prop: "prop value",
num: 42
}
};
cordova.plugins.notification.local.schedule(noteOptions);
localStorage.setItem(id, JSON.stringify(noteOptions));
navigator.notification.alert("Added notification id " + id + " will notify at: " + triggerDate.toString());
window.open("index.html");
}
}