在 Javascript 中将字符串转换为变量

发布于 2024-09-05 06:15:50 字数 932 浏览 3 评论 0原文

Javascript 有没有办法将字符串转换为变量?现在,我拥有的是一个根据选择框的值更新数据的脚本:

<script type="text/javascript">
var a = new Array();
a[0] = 'Name';
a[1] = 'Description';

var b = new Array();
b[0] = 'Name 2';
b[1] = 'Description 2';

function changeSystem(){
    var selectedAccount=document.getElementById('selected_option').value;

    document.getElementById('name').innerHTML = selectedAccount[0];
    document.getElementById('description').innerHTML = selectedAccount[1];
}
</script>
<form method="POST">
    <select onchange="changeSystem()" id="selected_option">
     <option>A</option>
     <option>B</option>
    </select>
</form>
<span id="name"></span><br>
<span id="description"></span><br>

selectedAccount

Is there any way to convert a string to a variable in Javascript? Right now, what I have is a script that updates data based on the values of the select box:

<script type="text/javascript">
var a = new Array();
a[0] = 'Name';
a[1] = 'Description';

var b = new Array();
b[0] = 'Name 2';
b[1] = 'Description 2';

function changeSystem(){
    var selectedAccount=document.getElementById('selected_option').value;

    document.getElementById('name').innerHTML = selectedAccount[0];
    document.getElementById('description').innerHTML = selectedAccount[1];
}
</script>
<form method="POST">
    <select onchange="changeSystem()" id="selected_option">
     <option>A</option>
     <option>B</option>
    </select>
</form>
<span id="name"></span><br>
<span id="description"></span><br>

selectedAccount is the string of the chosen element in <select>. However, for it to access the array, it needs to be a variable, i.e. a[0] instead of 'a'[0]. What solutions are there to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-09-12 06:15:50
var dict = {
    'A': ['Name', 'Description'],
    'B': ['Name 2', 'Description']
};

dict['A'][0]

动态访问它:

dict[selectedAccount][0]

因此,可以使用对象文字而不是数组来

var dict = {
    'A': {'name': 'john'}
};

dict['A']['name']
var dict = {
    'A': ['Name', 'Description'],
    'B': ['Name 2', 'Description']
};

dict['A'][0]

So dynamically access it with

dict[selectedAccount][0]

You can use an object literal instead of an array:

var dict = {
    'A': {'name': 'john'}
};

dict['A']['name']
痴骨ら 2024-09-12 06:15:50

我会使用类似的东西:

<script type="text/javascript">
var stuff = { 
    A: { name: 'Name', desc: 'Description'},
    B: { name: 'Name 2', desc: 'Description 2'}
};

function $(aId) {
    return document.getElementById(aId);
}

function changeSystem(){
    var selectedAccount = $('selected_option').value;

    if (selectedAccount in stuff) {
        $('name').innerHTML = stuff[selectedAccount].name;
        $('description').innerHTML = stuff[selectedAccount].desc;
    } else {
        $('name').innerHTML = '';
        $('description').innerHTML = '';
    } 
}
</script>
<form method="POST">
    <select onchange="changeSystem()" id="selected_option">
        <option>A</option>
        <option>B</option>
        <option>C</option>
    </select>
</form>
<span id="name"></span><br>
<span id="description"></span><br>

测试:Google Chrome 6.0.427.0 dev

I would use something like that:

<script type="text/javascript">
var stuff = { 
    A: { name: 'Name', desc: 'Description'},
    B: { name: 'Name 2', desc: 'Description 2'}
};

function $(aId) {
    return document.getElementById(aId);
}

function changeSystem(){
    var selectedAccount = $('selected_option').value;

    if (selectedAccount in stuff) {
        $('name').innerHTML = stuff[selectedAccount].name;
        $('description').innerHTML = stuff[selectedAccount].desc;
    } else {
        $('name').innerHTML = '';
        $('description').innerHTML = '';
    } 
}
</script>
<form method="POST">
    <select onchange="changeSystem()" id="selected_option">
        <option>A</option>
        <option>B</option>
        <option>C</option>
    </select>
</form>
<span id="name"></span><br>
<span id="description"></span><br>

Tested on: Google Chrome 6.0.427.0 dev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文