for(const id of this.extraRooms)
{
let doesRoomExist = document.getElementById('newRoomAddress' + newRoomId);
//if(typeof(doesRoomExist) !== "null")
if(typeof(doesRoomExist) !== "undefined")
{
const newRoomLabel = document.getElementById('newRoomAddress' + newRoomId).value
{
可能是一个简单的修复,但如果 doesRoomExist 为 NULL,它仍然会进入 if 语句...
let doesRoomExist = document.getElementById('newRoomAddress' + newRoomId);
在 VS 代码中单步执行代码时,会显示 doesRoomExist 为 NULL,但如果在开发人员工具中使用,则显示为未定义...为什么它会出现在 if 内部?
请问有什么帮助吗?
您应该直接检查
doesRoomExist
的值而不是其类型,您可以检查 if (doesRoomExist !== null)
或仅执行 if (doesRoomExist)
就足够了;
if(doesRoomExist !== null)
const newRoomLabel = document.getElementById('newRoomAddress' + newRoomId).value
只需使用
if (doesRoomExist) { /*...*/ }
。
for (const id of [1, 2, 3, 4, 5]) {
const doesExist = document.getElementById('item' + id);
if (doesExist) {
const newLabel = document.getElementById('item' + id).value;
console.log(newLabel);
}
}
<input id="item1" value="value1">
<input id="item2" value="value2">
<input id="item4" value="value4">
<input id="item5" value="value5">