我正在尝试将data
选项的动态键传递到ajax调用中。
data:{action:'add',id:10},
实际上,我正在尝试创建全局函数,它可以用于禁用特定行onClick
事件的所有表。为此,我需要添加不同的操作和表列名来调用php函数。所以我想传递键值。
JS函数
function globalDisable(id,table_column,call_action){
// call_action is use for calling the related php function
// table_column is use for calling the column name of that table
action_parm = {call_action:'disable',table_column:id}
$.ajax({
type:'POST',
url:'post.php',
data:action_parm,
success: function(response){
}
});
}
调用JS函数
onclick="return globalDisable(10,'user_id','disableUser');"
Php脚本
$disableUser = (isset($_REQUEST['disableUser']))? (string)$_REQUEST['disableUser']:'not';
if($disableUser != 'not'){
$response->disArea($user_id);
}
当我运行该函数时,控制台中不会更改其保持不变的call_action=disable&table_column=10
。
那么,在ajax调用中为数据选项动态传递密钥是可能的吗?如果有人指导我,我将不胜感激。谢谢
代替action_parm = {call_action:'disable',table_column:id}
,写入:
var action_param = {};
action_param[call_action]='disable';
action_param[table_column]='id';
请参阅:如何使用变量作为名称向JavaScript对象添加属性?
您将操作作为参数传递给函数,但在ajax调用中不使用它
function globalDisable(id,table_column,call_action){
// call_action is use for calling the related php function
// table_column is use for calling the column name of that table
//action_parm = {call_action:'disable',table_column:id}
var action_parm = {call_action: call_action,
table_column: table_column,
user_id: id};
$.ajax({
type:'POST',
url:'post.php',
data:action_parm,
success: function(response){
}
});
}
你可以这样做,也许你会发现阅读更容易
function globalDisable(id,table_column,call_action){
// call_action is use for calling the related php function
// table_column is use for calling the column name of that table
$.ajax({
type:'POST',
url:'post.php',
data:{ call_action: call_action,
table_column: table_column,
user_id: id
},
success: function(response){
}
});
}
同样在PHP中,您正在测试$_REQUEST 中的错误参数
$disableUser = (isset($_REQUEST['call_action']))? (string)$_REQUEST['call_action'] : 'not';
if($disableUser != 'not'){
$response->disArea($user_id);
}