我正在编写一个jQuery plugin ,我可以通过$.post方法发送数据,我想设置通过ajax对象传递的var名称的名称http://api.jquery.com/jQuery.post/ :
$('#selector').pluginName({url:'url.com/searches', var_name:'myVarName'});我的问题是我无法通过var_name设置名称,因为它作为var名称本身传递
var name = settings.var_name; $.post(settings.url, {name:next_search}, settings.callback);如果我发送此示例,我会发送{'name':'data'}而不是{'myVarName':'data'} 。
我怎样才能解决这个问题?
I'm writing a jQuery plugin where I can send data trough $.post method and I would like to set the name of the var name passed via ajax object http://api.jquery.com/jQuery.post/:
$('#selector').pluginName({url:'url.com/searches', var_name:'myVarName'});my problem is I can't set a name via var_name because it is passed as var name itself
var name = settings.var_name; $.post(settings.url, {name:next_search}, settings.callback);if I send this example i send {'name':'data'} instead of {'myVarName':'data'}.
how can I fix this?
最满意答案
jquery Ajax方法只为数据参数获取一个数组,并且有很多方法可以创建该数组。
如果要添加键在变量中的项目,则可以直接添加。
var data = {}; data[settings.var_name] = next_search; $.post( settings.url, data, settings.callback );The jquery Ajax method just takes an array for the data argument and there's lots of ways of creating that array.
If you want to add an item where the key is in a variable then you can add it directly.
var data = {}; data[settings.var_name] = next_search; $.post( settings.url, data, settings.callback );更多推荐
发布评论