How can i do this?
objPrefix = btn.attr('data-objprefix'); //<button data-objPrefix="foo">
var sendData = {objPrefix : {"bar":"ccccc"}};
ANd I want the output to be
{"foo" : {"bar":"ccccc"}};
but instead it is
{"objPrefix" : {"bar":"ccccc"}}
When you use the literal notation objPrefix
is considered as the keyname itself and not value of the variable objPrefix
, Instead try use bracket notation to set the property name for the object based on a variable value. So try this way:
var sendData = {};
sendData[objPrefix] = {"bar":"ccccc"};
Also you can infact use jquery data-api to fetch the value of data-attribute ie
objPrefix = btn.data('objprefix')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.