简体   繁体   中英

Custom JavaScript Object key

I am trying to modify the key for an AJAX POST within jQuery. If I replace the key with a variable, it seems to use the literal variable name instead of the value within.

$key = $(this).attr('id');

$.post( "php/register.php", { $key : $(this).val() }).done(function( data ) {

If the attribute was email I would expect to see:

email =>
string(15) "email@email.com"

Instead I get:

["$key"]=>
string(15) "email@email.com"

Does anybody know if what I'm trying to achieve is possible and how it can be done?

You can't use variables as a property like this. One alternative:

var data = {};
data[$key] = $(this).attr('id');

Then send the data

您必须在ajax帖子中传递要在php中处理的变量,将$ key替换为'email'。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM