admin管理员组文章数量:1401936
I am working in JavaScript coding. I have created a text area with name OQ_0
and value "0"
. When i use eval()
method for that field in JavaScript it is giving the value undefined
. The below are the part of JavaScript code
var tempOpenQtyStr = "document.InitiateReturnsForm.OQ" + "_" + 0;
var tempOpenxQtyStr = eval(tempOpenQtyStr).value;
alert('Manuals =' + document.InitiateReturnsForm.OQ_0.value);
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Output:
Manuals = 0
eval(tempOpenxQtyStr ) = 0 --- Here it is suppose to show "[object]"
eval(tempOpenxQtyStr).value = undefined.
Kindly help me out what is change to do. Thanks in advance.
I am working in JavaScript coding. I have created a text area with name OQ_0
and value "0"
. When i use eval()
method for that field in JavaScript it is giving the value undefined
. The below are the part of JavaScript code
var tempOpenQtyStr = "document.InitiateReturnsForm.OQ" + "_" + 0;
var tempOpenxQtyStr = eval(tempOpenQtyStr).value;
alert('Manuals =' + document.InitiateReturnsForm.OQ_0.value);
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Output:
Manuals = 0
eval(tempOpenxQtyStr ) = 0 --- Here it is suppose to show "[object]"
eval(tempOpenxQtyStr).value = undefined.
Kindly help me out what is change to do. Thanks in advance.
Share Improve this question edited Mar 10, 2010 at 12:51 Joachim Sauer 309k59 gold badges567 silver badges622 bronze badges asked Mar 10, 2010 at 12:45 rajaraja 212 silver badges3 bronze badges 2- 1 Could you please post the html part relating to this, just so that we can see the DOM, Thanks – Mahesh Velaga Commented Mar 10, 2010 at 12:50
- Stop using eval()!! This whole thing is a bad idea. – Pointy Commented Mar 10, 2010 at 13:43
3 Answers
Reset to default 5Why not just use document.InitiateReturnsForm["OQ_" + 0].value
?
Try
alert('eval(tempOpenxQtyStr ) = ' + eval(tempOpenQtyStr));
alert('eval(tempOpenxQtyStr).value = ' + eval(tempOpenQtyStr).value);
In the second and third alert you are evaluating the second variable which stores the value of the first evaluated object. That's why the error occurs.
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
Since you put a string, not an object, inside tempOpenxQtyStr, it evaluates that string and returns 0.
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Here you're using a method on a variable that contains a string. That doesn't work. It doesn't have that method, that's why it returns undefinied.
You might want to try doing eval(tempOpenxQtyStr.value)
instead of eval(tempOpenxQtyStr).value
since the last one does basically nothing, just evaluating an object and then fetching the objects value (it doesn't eval the value itself).
本文标签: Why does eval() give undefined value in JavascriptStack Overflow
版权声明:本文标题:Why does eval() give undefined value in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744269586a2598117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论