简体   繁体   English

Javascript 使用来自 HTML 输入字段的值时中断

[英]Javascript breaks when using values from HTML input field

I'm trying to create a grid of a specific height/width given input.我正在尝试创建给定输入的特定高度/宽度的网格。 If I hard code in the height and width in the canvas below (10 and 5 in this case) it works perfectly如果我在下面的 canvas 中对高度和宽度进行硬编码(在本例中为 10 和 5),它会完美运行

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {

    const canvas = new Canvas(document.getElementById("canvas-window"), 10, 5);
    canvas.generateCanvas();
})

This example breaks though:这个例子打破了:

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {
    const canvas = new Canvas(document.getElementById("canvas-window"), height.value, width.value);
    canvas.generateCanvas();
})

Any ideas what could be causing this?任何想法可能是什么原因造成的? If I log height.value and width.value it is getting the values just fine, so I'm struggling to see how it's any different than being hardcoded if the values are being read in fine?如果我记录 height.value 和 width.value 它得到的值就很好,所以我正在努力看看它与硬编码有什么不同,如果这些值被正常读取的话? The error I'm getting is:我得到的错误是:

canvas.js:18 Uncaught TypeError: Cannot set properties of undefined (setting '0') at Canvas.generateCanvas (canvas.js:18:33) at HTMLInputElement. canvas.js:18 未捕获类型错误:无法在 Canvas.generateCanvas (canvas.js:18:33) 在 HTMLInputElement 处设置未定义的属性(设置“0”)。 (pixelcreator.js:8:12) (pixelcreator.js:8:12)

Here is code containing the line that the error references:这是包含错误引用的行的代码:

class Canvas {

grid;

constructor(window, height, width) {
    this.window = window;
    this.height = height;
    this.width = width;
}

generateCanvas() {
    this.grid = new Array(this.height).fill(0).map(() => new Array(this.width).fill(0));
    for (let i = 0; i < this.height; i++) {
        const row = document.createElement("div");
        row.classList.add('row');
        this.window.appendChild(row);
        for (let j = 0; j < this.width; j++) {
            this.grid[i][j] = new Cell(row) // this is line 18 from the error
            this.grid[i][j].createCell();
        }
    }
}

height.value or width.value might be a string type. height.valuewidth.value可能是字符串类型。

Try this:尝试这个:

new Canvas(document.getElementById("canvas-window"), parseInt(height.value), parseInt(width.value));

You are using the value of the input directly which is a string.您直接使用输入的值,它是一个字符串。 Try parsing it to an integer with parseInt(...)尝试使用 parseInt(...) 将其解析为 integer

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {
    const canvas = new Canvas(document.getElementById("canvas-window"), parseInt(height.value), parseInt(width.value));
    canvas.generateCanvas();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM