简体   繁体   English

如何向HTML表格单元格添加唯一命名的JavaScript函数

[英]How to add to HTML table cells a uniquely named JavaScript function

I have a Sudoku table which I've generated using JavaScript. 我有一个使用JavaScript生成的Sudoku表。 After generating the table, I've also attached a number entry field as well as a function to each cell. 生成表格后,我还为每个单元格附加了一个数字输入字段以及一个函数。 Currently, the function passes in only the very last cell id that was generated (ex. R9C9), not the cell that was recently modified. 当前,该函数仅传入生成的最后一个单元格ID(例如R9C9),而不传入最近修改的单元格。 I would rather have the function get passed the current cell id which the user modified so that I can check the row, column, and current square for conflicts and not every single cell on the board. 我宁愿让函数传递用户修改的当前单元格ID,以便我可以检查行,列和当前正方形是否存在冲突,而不是检查板上的每个单元格。 I'm trying not to use jQuery, but any help would be appreciated. 我试图不使用jQuery,但任何帮助将不胜感激。

This is the code that attaches the number field, as well as the function. 这是附加数字字段和功能的代码。 I believe currently the function it attaches is anonymous, which wouldn't matter as long as I could get the attached anonymous function to be associated with the current cell. 我相信当前附加的函数是匿名的,只要我可以将附加的匿名函数与当前单元格关联就没有关系。

for (i = 1; i <=9; i++)
{
    for (j = 1; j <= 9; j++) {
        temp = document.getElementById('R'+i+'C'+j);
        if (temp.innerHTML == "")
        {
            var temp2 = temp.appendChild(document.createElement("input"));
            temp2.type = "number";
            temp3 = document.getElement('R'+i+'C'+j);
            temp2.onkeyup = function(){conflictChecker(temp3)};
        }
    }
}

This is the code for generating the table. 这是用于生成表的代码。 function makeGrid(x, y, name) { 函数makeGrid(x,y,name){

var tableDiv = document.getElementById("gameTable");
var table = document.createElement('table');
table.id = "theTable";



table.style.width = '100px';
table.style.border = 'thick solid black';

for (var i = 1; i <= x; i++) {
    var row = table.insertRow();
    row.id = "Row" + i;

    for (var j = 1; j <= y; j++) {
        var cell = row.insertCell();
        cell.id = "R" + i + "C" + j;
        cell.classList.add();
        cell.style.border = '1px solid';
        if((i%3) == 0)
        {
            cell.style.borderBottom = "thick solid black";
        }
        if(((j-1)%3) == 0)
        {
            cell.style.borderLeft = "thick solid black";
        }

    }



}
tableDiv.appendChild(table)

The original elements of the table I add using so the user cannot modify them. 我添加使用的表的原始元素,因此用户无法修改它们。

document.getElementById("R_C_").innerHTML = "someNumber";

I tried taking the assingment of the functions out of the orinal function by adding this 我尝试通过添加此功能将功能的定义从常规功能中删除

for (i =1; i <= 9; i++) {

for (j = 1; j <= 9; j++) {
    temp = document.getElementById('R' + i + 'C' + j);
    temp.onkeyup = function(){conflictChecker(temp)};
}

}

Any help would be immensely appreciated. 任何帮助将不胜感激。 I've tried several different types of syntax, but they don't seem to be working. 我尝试了几种不同类型的语法,但是它们似乎没有用。

What's happening is that the javascript is not computing the value of temp3 - it's keeping it as a variable until the function is called, and then it computes the value, which will be equal to what it was during the very last iteration... 发生的事情是javascript没有计算temp3的值-它将其保留为变量,直到调用该函数,然后才计算该值,该值将等于最后一次迭代时的值。

Did you try this? 你有尝试过吗?

temp2.onkeyup = function(){conflictChecker(this.parent)};

I solved my problem. 我解决了我的问题。 For some reason using the onkeyup function for my event wasn't working. 由于某种原因,无法为我的活动使用onkeyup函数。 I switched it to onchange() and was able to get the current cell by passing in 'this' 我将其切换为onchange()并能够通过传递“ this”来获取当前单元格

for (i = 1; i <=9; i++)
{
    for (j = 1; j <= 9; j++) {
        temp = document.getElementById('R'+i+'C'+j);
        if (temp.innerHTML == "")
        {
            var temp2 = temp.appendChild(document.createElement("input"));
            temp2.type = "number";
            temp2.id = 'R'+i+'C'+j+'2';
            temp2.onchange = function(){conflictChecker(this)};
        }
        else
        {
            theVal = temp.innerHTML;
            temp.value = theVal;
            temp.id = 'R'+i+'C'+j+'2';
        }
    }
}

I assigned different Id's as well so that I could look up the value of the cell from conflictChecker. 我还分配了不同的ID,以便可以从冲突检查器中查找单元格的值。 Probably not the most elegant solution but it works. 可能不是最优雅的解决方案,但它可行。

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

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