[英]Error with Javascript TypeError code, variable undefined
I am trying to get the element with the ID 1a
, 2a
, 3a
etc. according to whenever the function is run.我试图根据 function 的运行时间来获取 ID 为1a
、 2a
、 3a
等的元素。
It then compares that elements value (using jQuery) with the value of the input
where the function is wrong It brings up an error saying:然后,它将元素值(使用 jQuery)与 function 错误的input
值进行比较它提出了一个错误说:
TypeError: var1.toUpperCase is not a function.类型错误:var1.toUpperCase 不是 function。 (in 'var2.toUpperCase()','var1.toUpperCase' is undefined) (在 'var2.toUpperCase()' 中,'var1.toUpperCase' 未定义)
Any help would be appreciated.任何帮助,将不胜感激。 Thanks (UPDATE usually there would be text in questionNumber like: 1, 2, 3 etc every time the another function is run.)谢谢(每次运行另一个 function 时,更新通常都会出现问题文本,例如:1、2、3 等。)
EDIT: Every time a different function is run, questionNumber
is increased by 1. I save questionNumber
's text in a variable called word
.编辑:每次运行不同的 function 时, questionNumber
都会增加 1。我将questionNumber
的文本保存在一个名为word
的变量中。 I then add the letter a
to that variable.然后我将字母a
添加到该变量中。 Then, I get the element that has ID of the variable word
, then compare it's contents to the value of the input
, but the comparison is uppercase to avoid problems.然后,我得到具有变量word
的 ID 的元素,然后将其内容与input
的值进行比较,但比较是大写以避免出现问题。 If they are equal, the input
is replaced with a div
with green text.如果它们相等,则input
将替换为带有绿色文本的div
。 Hope this makes it clearer.希望这能让它更清楚。
function textVerify(item) { var word= document.getElementById(($('#questionNumber').text()+'a')); if (item.value.toUpperCase() === word.toUpperCase()){ item.style.color = "green"; $( item ).replaceWith( "<div style='color:green;'>"+word+"</div>" ); main() } else { item.style.color = "black"; }
<span class="ihide" id="questionNumber"></span> <p id="1a" class="ihide">Seven</p> <input id="1" name="Seven" type="text" value="" onkeyup="textVerify(this)" autofocus="">
The var word
is p
tag, so you need to get the inner text of it and compare it with the input text. var word
是p
标签,所以你需要获取它的内部文本并与输入文本进行比较。 Also, when replacing it, access the text()
property of it.此外,在替换它时,访问它的text()
属性。 See below.见下文。 main()
is commented out here, but you can keep as per the need. main()
在这里被注释掉了,但是你可以根据需要保留。
function textVerify(item) { var word = document.getElementById(($('#questionNumber').text() + 'a')); if (item.value.toUpperCase() === $(word).text().toUpperCase()) { item.style.color = "green"; $(item).replaceWith("<div style='color:green;'>" + $(word).text() + "</div>"); //main() } else { item.style.color = "black"; } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <span class="ihide" id="questionNumber">1</span> <p id="1a" class="ihide">Seven</p> <input id="1" name="Seven" type="text" value="" onkeyup="textVerify(this)" autofocus="">
In your code ($('#questionNumber').text()+'a')
this part returns just 'a', as text of the id questionNumber
is nothing.在您的代码($('#questionNumber').text()+'a')
中,这部分只返回 'a',因为 id questionNumber
的文本什么都不是。
And in your HTML there is no such id.在您的 HTML 中没有这样的 id。 I think you need to make this changes to your HTML code:我认为您需要对 HTML 代码进行此更改:
<span class="ihide" id="questionNumber">1</span>
This should work.这应该有效。
EDIT: Also, can you please share the JS code associated with 'item', there can be an error in that part too.编辑:另外,您能否分享与“项目”相关的 JS 代码,那部分也可能有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.