简体   繁体   中英

Javascript eval

I am trying to get the following working. It seemed to work initially, but somehow it stopped working

var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + eval("setCommonAttr")).value;

what is wrong with above?

The above code is little different from what I am trying to accomplish. I gave the above example just not to make things complicated here. Below is what I am trying to accomplish:

First I am getting an existing element as follows. The element is a

<tr id="row_1_4_2009_abc" class="rowclick">
   <td></td>
</tr>

I am using jquery to get the id on click of a row:

$(".rowclick").click(function() {
  var row_id = $(this).attr("id");
  var getAttributes = row_id.split("_");
  var setCommonAttr = getAttributes[1] + "_" + getAttributes[2] + "_" + getAttributes[3] + "_" + getAttributes[4];
  var new_row_id = document.getElementById("new_row_" + setCommonAttr).value;
});

You shouldn't need eval() to do this. The value you want is already a variable in JavaScript. Try:

var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + setCommonAttr).value;

You don't need to call eval().

You can just concatenate the string with the variable:

var setCommonAttr = "1_row1_common"; var val = document.getElementById("abc_" +   setCommonAttr).value;

Will everything be in that form? row_xxx_xxx_xxx_xxx

if so, why not var new_row_id = document.getElementById("new_" + row_id).value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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