1

我对 Javascript 有点陌生...我需要做的是通过它的名称获取一个变量,代码可以将其作为字符串获取,但该字符串会根据输入等而有所不同。我的搜索没有到目前为止还没有得到任何东西,但也许我不知道用来描述这个的正确术语。这是我到目前为止所做的:

var 1name = 'test';                          // This part is added by php, and
var 1data = 'THIS IS TEST TABLE RAWR';       // the variable names end up being
var 2name = 'etc';                           // a number followed by "name", etc.
var 2data = 'MORE PLACEHOLDER TEXT HERE';

稍后的:

function editTable(id) {
if (id != null) {
    document.getElementById('name').value = window[id + "name"];        
    }
}

和一个按钮:

<input type='button' value='Edit'  onclick="editTable(document.getElementById('table').value)" />

该按钮获取表单字段的值,该值将匹配从 php.ini 添加的一组变量的数量。我怎样才能让它工作?我已经确保变量被插入到页面中,并且按钮获得了匹配的值。你能明白我的目的吗?或者你需要更多解释吗?

4

1 回答 1

1

Variables cannot start with numbers. Personally I think you should use an array like this:

var data = [
    {name:"text",data:"THIS IS TEST TABLE RAWR"},
    {name:"etc",data:"MORE PLACEHOLDER TEXT HERE"}
];

Since you've mentioned PHP outputting this, look at json_encode. Then you could just have:

var data = <?php echo json_encode($data); ?>;

Assuming, of course, that $data is your array structure.

于 2013-03-17T21:51:50.980 回答