[英]Multidimensional Array Not Updating (or is it…?)
I have the most peculiar issue I can't seem to solve without writing band-aid code. 如果没有编写创可贴代码,我似乎无法解决最奇怪的问题。
FULL SOURCE: http://sinsysonline.com/tictactoe_test.html or Fiddle: http://jsfiddle.net/JsXEP/ 完整资源: http : //sinsysonline.com/tictactoe_test.html 或小提琴: http : //jsfiddle.net/JsXEP/
I am using a multidimensional array
for the JS and a table
for the HTML for data and display purposes. 我正在为JS使用
multidimensional array
,并为HTML使用table
用于数据和显示目的。
So, our data will look something like this for JS : 因此,对于JS ,我们的数据如下所示:
var b = [ ["","",""], ["","",""], ["","",""] ];
The table will be a standard table with some CSS tweaks for our HTML : 该表将是标准表,并对HTML进行一些CSS调整:
<table id="board">
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
So, here is the problem. 所以,这就是问题所在。 The input works just fine, resetting the game appears to work fine, but let's make a
use-case
so I can demonstrate the problem. 输入效果很好,重置游戏似乎效果很好,但是让我们举个
use-case
以便我演示问题。
If I utilize a grid size
of 3
initially, then try to make a game with a grid size
of 4
without refreshing the page, the data seems to display at the top right for my array
that the code is working as intended. 如果我最初使用的
grid size
为3
,然后尝试制作一个grid size
为4
的游戏而不刷新页面,则数据似乎显示在我的array
右上角,表明代码正在按预期工作。
However, when you console.log(b)
, it never actually updates b
and anything after the first three rows
in the table don't respond. 但是,当您
console.log(b)
,它实际上不会更新b
并且在表的three rows
没有响应之后,任何东西都不会更新。
I'm given a console error of: 我收到控制台错误:
TypeError: b[row] is undefined
Why isn't b
actually updating with the increased value, even though I define it as var b=[];
为什么不
b
与增加的值实际更新,即使我把它定义为var b=[];
at the beginning of my click-handler
for #go
? 在我的
#go
click-handler
的#go
?
And why is my #alert
box for debugging filling in the correct b
value? 为什么我的
#alert
调试框填写正确的b
值?
Scratches Head 抓头
Any help is appreciated... 任何帮助表示赞赏...
HTML: HTML:
<div id="dashboard">
<p>How large is your grid? (3-10)</p>
<input type="text" id="size" size="1" />
<p>Which icon would you like?</p>
<select name="mydropdown" id="mark">
<option value="check">Check-Mark</option>
<option value="x">Traditonal X</option>
<option value="o">Traditional O</option>
</select>
<h3 id="title">Troubleshooting Console</h3>
<input type="button" id="go" value="Create Board / Reset" />
<p id="alert">Alerts Live Here</p>
</div>
<table id="board">
</table>
Javascript: 使用Javascript:
$("#go").click(function () {
var b=[],
s = parseInt($("#size").val()),
v = $("#mark").val();
if (s<3 || s>10) { alert("That is not a valid input. Please select 3-10"); return; }
$('#board tr').remove();
$('#alert').text("Your Turn!");
for (var i = 0; i < s; i++) {
var t = [];
var $tr = $('<tr />').data('index', i + 1);
for (var j = 0; j < s; j++) {
t[j] = "";
$('<td />').data('index', j + 1).appendTo($tr);
}
b.push(t);
$("#board").append($tr);
}
$("#board").on('click', 'td', function () {
var $td = $(this),
td = $td.data('index')-1,
row = $td.parent().data('index')-1;
b[row][td] = "X";
$td.removeClass().addClass(v);
$('#alert').text(b);
});
});
FULL SOURCE: http://sinsysonline.com/tictactoe_test.html or Fiddle: http://jsfiddle.net/JsXEP/ 完整资源: http : //sinsysonline.com/tictactoe_test.html 或小提琴: http : //jsfiddle.net/JsXEP/
The problem is because of the closure property and multiple definitions of on click function. 问题在于闭包属性和on click函数的多个定义。
Updated Fiddle
$("#board").on('click', 'td', function () {
var $td = $(this),
td = $td.data('index')-1,
row = $td.parent().data('index')-1;
b[row][td] = "X";
$td.removeClass().addClass(v);
$('#alert').text(b);
});
This function is registered every time user changes the settings and whenever it is defined, it captures the b
. 每次用户更改设置并定义该功能时,都会捕获该
b
。 So, first time, lets say, b
is [3][3] and the on click function is created with that. 因此,首先,让我们说
b
是[3] [3],并由此创建了on click函数。 Next time, b
is [4][4] and a new on click function is created (old one still exists, with b
[3][3]). 下次,
b
为[4] [4],并创建了一个新的单击功能(旧的仍然存在,带有b
[3] [3])。 So both of them are fired now onwards. 所以他们两个都被解雇了。 If I select anything outside 3 x 3, the old one fails.
如果我选择3 x 3以外的任何内容,旧的将失败。 That's why you are facing this problem.
这就是为什么您要面对这个问题。
So, I just moved the definition of on click function, b
and v
outside and it works fine now. 因此,我只是将on click函数
b
和v
的定义移到了外部,现在可以正常使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.