通过变量设置对象键 [英] Setting object keys from variables

查看:54
本文介绍了通过变量设置对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在JavaScript中构造一个对象文字,其表达式的值等于属性名称的字符串?

Possible Duplicate:
in javascript is it possible to construct an object literal with expressions evaluating to strings for property names?

我在设置变量中的对象键和值时遇到问题,如果我只设置键或仅从变量中设置值,则如果我都从变量中设置它们都无法工作,则可以正常工作.

I am having a problem setting object keys and values from variables, if I set just the key or just the value from a variable it works fine if I set them both from variables it does not work.

这很好

 $(":input").change(function () { 

    var currentId = $(this).attr('id');
    var currentval = $(this).attr('value');

    $.post("/inside/update.php", {
        "profile_age":currentval 
    });
});

此配置不起作用

$(":input").change(function () { 

    var currentId = $(this).attr('id');
    var currentval = $(this).attr('value');

    $.post("/inside/update.php", {
        currentId:currentval 
    });
});

推荐答案

尝试一下:

var options = {};
options[currentId] = currentval; 
$.post("/inside/update.php", options);

这篇关于通过变量设置对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆