从变量设置对象键
可能重复:
在javascript中是否可以使用表达式来评估属性名称的字符串来构造对象文字?
我在设置变量的对象键和值时遇到问题,如果我只设置键或只是变量中的值,如果我从变量设置它们都不起作用它就可以正常工作。
这很好用
$(":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);