我不知道这是否可以做到,我有一个带有提交的输入:
Variable JSON (data/data.links)
<input type="text" id="newVarJson" value="data" onclick="verifica();"/><br>
我需要将该输入的值作为 javascript 中变量的名称来创建具有该名称的 json,我想这样做:
<script type="text/javascript">
function verifica(){
var document.getElementById(newVarJson).value={}
data.value="asdf";
alert(JSON.stringify(data));
}
</script>
正如你所看到的,变量输入值=数据,我不知道这是否可以做到,所以,有人可以告诉我任何提示来做到这一点,或者它不能做到吗?
谢谢!
最佳答案
你可以试试这个:
window[document.getElementById(newVarJson).value];
你的变量名称将是该输入的值。
编辑:
window[document.getElementById('newVarJson').value];
工作 jsbin: http://jsbin.com/doparibu/1/edit
关于javascript - 使用输入值创建变量 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709219/