简体   繁体   English

Javascript评估

[英]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: 我使用jquery获取点击一行的id:

$(".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. 你不应该需要eval()来做这件事。 The value you want is already a variable in JavaScript. 您想要的值已经是JavaScript中的变量。 Try: 尝试:

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

You don't need to call eval(). 您不需要调用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 row_xxx_xxx_xxx_xxx

if so, why not var new_row_id = document.getElementById("new_" + row_id).value; 如果是这样,为什么不var new_row_id = document.getElementById("new_" + row_id).value;

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

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