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
Add a ment  | 

3 Answers 3

Reset to default 5

Why 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