I am unsure which of them is wrong because you did not provide your HTML, but one of these does not exist:
(我不确定哪一个是错的,因为你没有提供你的HTML,但其中一个不存在:)
var str = document.getElementById("cal_preview").value;
var str1 = document.getElementById("year").value;
var str2 = document.getElementById("holiday").value;
var str3 = document.getElementById("cal_option").value;
There is either no element with the id cal_preview
, year
, holiday
, cal_option
, or some combination.
(没有具有id cal_preview
, year
, holiday
, cal_option
或某种组合的元素。)
Therefore, JavaScript is unable to read the value of something that does not exist.
(因此,JavaScript无法读取不存在的值的值。)
EDIT :
(编辑 :)
If you want to check that the element exists first, you could use an if statement for each:
(如果要先检查元素是否存在,可以为每个元素使用if语句:)
var str,
element = document.getElementById('cal_preview');
if (element != null) {
str = element.value;
}
else {
str = null;
}
You could obviously change the else statement if you want or have no else statement at all, but that is all about preference.
(如果你想要或根本没有其他声明,你显然可以更改else语句,但这完全取决于首选项。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…