Get element's ID and set it as variable

前端 未结 6 1476
执笔经年
执笔经年 2020-12-02 02:18

I have a button:

相关标签:
6条回答
  • 2020-12-02 02:20

    Here's 3 options for you.

    eval (not recommended)

    You can use the Javascript function eval to achieve what you want. But be aware, this is 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 consists of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overridden by other JS files, which is bad. If you want to know more about 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 this technic, you would do something like:

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

    Using an object (recommended)

    The best solution would be to create your 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 your dynamic variables. It works 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
    }
    
    0 讨论(0)
  • 2020-12-02 02:23

    Use eval (but not recommended):

    var id = 'that_id';
    eval(id+" = 'other id';");
    alert(that_id); // results: 'other id'
    
    0 讨论(0)
  • 2020-12-02 02:32

    You can use and object to store your variables in:

    var variables = {};
    

    to add a variable just type:

    variables[name] = value;
    

    and to access the value:

    variables[name] 
    

    Check it out here: jsFiddle

    Button 2 reads the value of variables[formid] and button 1 sets formid to submitted

    0 讨论(0)
  • 2020-12-02 02:34

    Your best option for archieving what you really want is using eval(), like this:

    eval("var " + currForm.attr('id');
    

    Check this link ->

    Dynamic Variables in Javascript

    0 讨论(0)
  • 2020-12-02 02:41

    Try one of the following:

    $(this).attr(example_id, "value")
    

    Or

    window[ currForm.attr(id)] = 12;
    

    Or

    window[ this.id ] = 12;
    
    0 讨论(0)
  • 2020-12-02 02:42

    You can do it using javascript object:

    var currForm = $(this).parents('form');//form id="hello"
    
    var obj = {};
    
    obj[currForm.attr('id')] = 30;
    
    console.log(obj.hello)
    
    0 讨论(0)
提交回复
热议问题