简体   繁体   English

在JavaScript中将第二个变量值保持“第一个”

[英]Keeping second variable value “one behind” first variable value in javascript

I am setting up a bingo call board system for my local senior center on a laptop (without Internet access) to be used through a screen projector system. 我正在为我的本地高级中心在便携式计算机(没有Internet访问)上设置宾果电话留言板系统,以通过屏幕投影仪系统使用它。 I am doing it in a locally saved html file to open in a webbrowser, and I already have most of the system working together fine. 我正在本地保存的html文件中执行此操作,以便在网络浏览器中打开它,并且我已经可以正常使用大多数系统了。

For the "all called balls" display, I have an HTML table setup with all the bingo numbers as clickable fields to input into a variable. 对于“众人皆知的球”显示,我有一个HTML表设置,其中将所有宾果游戏编号作为可点击字段输入到变量中。 This is done through jquery by using the following: 这是通过使用以下命令通过jquery完成的:

$(document).ready(function() {
$("tbody td").click(function(e) {

var currentCellText = $(this).text();

I then add the correct BINGO letter prefix to the called number by using this: 然后,使用以下命令将正确的BINGO字母前缀添加到被叫号码:

if (currentCellText < 16)
{
    var bingoletter = "B";
}
else if (currentCellText < 31 && currentCellText > 15)  
{  
    var bingoletter = "I";  
}  
else if (currentCellText < 46 && currentCellText > 30)
    var bingoletter = "N";  
}  
else if (currentCellText < 61 && currentCellText > 45)  
{  
    var bingoletter = "G";  
}  
else if (currentCellText < 76 && currentCellText > 60)  
{  
    var bingoletter = "O";  
}  
else  
{  
    var bingoletter = ""; 
    var currentCellText = "";
}  


var currentnumber = ("<BIG><BIG><BIG><BIG><BIG><B><CENTER>" + bingoletter + currentCellText + "<br/>");

document.getElementById("currentnumber").innerHTML = currentnumber; 

I have to include the HTML code here as if I try to put it into the actual table, everytrhing breaks. 我必须在此处包括HTML代码,就像我试图将其放入实际表中一样,一切都中断了。 Not sure why... 不知道为什么

I then display the called number by using this table cell in a separate table display (not an alert box): 然后,通过在单独的表格显示(不是警报框)中使用此表格单元格来显示被叫号码:

<td id="currentnumber" width="50%" rowspan="2"></td>

I also have a separate javascript that also changes the background color of each table cell when I click on the number so as to have a display of numbers already called. 我还有一个单独的javascript,当我单击数字时,它也会更改每个表格单元格的背景颜色,以便显示已经被调用的数字。 This works fine, along with the jquery that displays the currently called number in a larger font display separately. 这和将以更大的字体显示分别显示当前被叫号码的jquery一起工作正常。

Everything is working fine, but what I am stuck on is trying to get another, second, variable that would display the last number called. 一切工作正常,但我遇到的问题是尝试获取另一个第二个变量,该变量将显示最后一个调用的数字。 What this has to do is display the last number called when a new number is clicked upon as the current number called. 要做的是显示在单击新号码时显示的最新号码作为当前号码。 I have a table display setup, all I have to figure out is the proper coding to be one behind the current. 我有一个表格显示设置,我需要弄清楚的是正确的编码,可以落后于当前。 In other words, this second variable would hold the value of the current number called variable "one behind" the actual current number variable. 换句话说,第二个变量将保存当前数字的值,称为当前实际数字变量“后一个”。 ie I click on B6 in the html table so B6 background changes color in the main display table and B6 is displayed in a separate called number table cell (all works fine up to here) and the "last number called" cell is blank as no prior numbers have been called yet, when I click on I21 as the current number called, I want B6 to show up in the Last Number called variable. 即我单击html表中的B6,因此B6背景会在主显示表中更改颜色,并且B6显示在单独的被称为“数字表”单元格中(到这里为止一切正常),“最后被叫”单元格为空,因为没有先前的编号已被调用,当我单击I21作为当前编号时,我希望B6显示在称为变量的最后一个编号中。 And so on as G60 is clicked on to be the next current number, then last number should show I21. 依次类推,依次单击G60作为下一个当前数字,则最后一个数字应显示I21。

Many thanks in advance, Stan... 预先感谢,斯坦...

Try this: 尝试这个:

document.getElementById("lastnumber").innerHTML = document.getElementById("currentnumber").innerHTML;

immediately before you set the contents of currentnumber . 在设置currentnumber的内容currentnumber The idea is just to copy whatever you were showing last just before you change it the new text. 这个想法只是在更改新文本之前复制您最后显示的内容。 Obviously, you'll need to change lastnumber to whatever you've called the cell you want the last number called in. 显然,您需要将lastnumber更改为您要调用的最后一个数字所在的单元格。

You need to store the currentCellText outside the click function so that it retains its value. 您需要将currentCellText存储在click函数之外,以便保留其值。 Initialise it to an empty string to start with. 将其初始化为一个空字符串。

$(document).ready(function() {

var currentCellText = "";

$("tbody td").click(function(e) {

Then create a second variable (let's say lastCellText ). 然后创建第二个变量(假设为lastCellText )。 Again initialise this to an empty string outside of the click function. 再次将其初始化为click函数外部的空字符串。

$(document).ready(function() {

    var currentCellText = "";
    var lastCellText = "";

    $("tbody td").click(function(e) {

Then in the click function, simply store the value of currentCellText into lastCellText before updating the value of currentCellText with $(this).text() 然后在click函数中,只需将currentCellText的值存储到lastCellText然后再使用$(this).text()更新currentCellText的值。

$("tbody td").click(function(e) {

    lastCellText = currentCellText;
    currentCellText = $(this).text();

Hope this helps. 希望这可以帮助。

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

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