du6333137 2014-09-24 14:36
浏览 65
已采纳

获取元素的ID并将其设置为变量

I have a button:

<button class="btn btn-info continue">
    <i class="ace-icon fa fa-check bigger-110"></i>
    Continue                                        
</button>

Onclick:

$(".continue").click(function(e) {
    currForm = $(this).parents('form');
    e.preventDefault();
});

I can get the id pretty easily: currForm.attr('id');, but can I set the value of this id as a variable.

Something like php's variable variables:

$a = 'hello';
$$a = 'world';
echo $hello;

Edit: I don't want to change element's id. I want to get this ID and use it as a name for a javascript variable. For example, the element I provided above is in a form that has ID='example_id'. currForm.attr('id') will give me example_id and I want to set a variable example_id and assign a value to it.

var example_id = 'some value';

  • 写回答

6条回答 默认 最新

  • douxian6086 2014-09-24 14:51
    关注

    Here 3 options for you.

    eval (not recommended)

    You can use the Javascript function eval to achieve what you want. But be aware, this in not recommended (I emphasized it twice, hope you understood!).

    Don't use eval needlessly!

    eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

    It would be used like that :

    eval('var ' + varName + ' = "something";');
    

    Window object notation (Better than eval, still not really recommended)

    This method consist of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overrided by other JS files which is bad. If you want to know more on that subject, this is a good question : Storing a variable in the JavaScript 'window' object is a proper way to use that object?.

    To use that method, you would do thing like that :

    window[varName] = something;
    alert(window[varName]);
    

    Using an object (recommended)

    The best solution would be to creating you own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create you dynamics variables. It work the same way as the window does :

    var globalScope = {};
    
    function awesome(){
        var localScope = {};
        globalScope[varName] = 'something';
        localScope[varName] = 'else';
    
        notSoAwesome();
    }
    
    function notSoAwesome(){
        alert(globalScope[varName]); // 'something';
        alert(localScope[varName]); // undefined
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程