I have a function named update that takes some options and post to my php using $.post:
我有一个名为update的函数,它接受一些选项并使用$ .post发布到我的php:
function update(options){
$.post('update.php',{update: options}, function(data, textStatus, jqXHR){
// debug stuff from php
});
}
I call my function as follows:
我把我的功能称为如下:
update({'id': id, 'option': val});
What I would like to know is how I can make a conditional inside the options? For example:
我想知道的是我如何在选项中制定条件?例如:
var option = $(this).attr('class') == 'check' ? 'check' : 'ok'; // assuming option is ok
update({'id': id, 'ok': val}); // instead of option
update({'id': id, option: val}); // i want to have the conditional
2 个解决方案
#1
4
You can access Javascript objects in the same way that you access arrays. This syntax supports dynamic property names/keys.
您可以像访问数组一样访问Javascript对象。此语法支持动态属性名称/键。
options[variable] = 'value';
Example:
例:
var x = 'customProperty'
, y = {}
;
y.x = 12; //y.x = 12
y[x] = 11; //y.customProperty = 11... same as y['customProperty'] = 11
http://jsfiddle.net/CoryDanielson/Dugdd/
http://jsfiddle.net/CoryDanielson/Dugdd/
#2
0
Essentially you set your object as such:
基本上你设置你的对象:
var x = {};
Then assign the exact property you want
然后分配您想要的确切属性
var prop_name = $(this).attr('class') == 'check' ? 'check' : 'ok';
x[prop_name] = YOUR_VALUE;
#1
4
You can access Javascript objects in the same way that you access arrays. This syntax supports dynamic property names/keys.
您可以像访问数组一样访问Javascript对象。此语法支持动态属性名称/键。
options[variable] = 'value';
Example:
例:
var x = 'customProperty'
, y = {}
;
y.x = 12; //y.x = 12
y[x] = 11; //y.customProperty = 11... same as y['customProperty'] = 11
http://jsfiddle.net/CoryDanielson/Dugdd/
http://jsfiddle.net/CoryDanielson/Dugdd/
#2
0
Essentially you set your object as such:
基本上你设置你的对象:
var x = {};
Then assign the exact property you want
然后分配您想要的确切属性
var prop_name = $(this).attr('class') == 'check' ? 'check' : 'ok';
x[prop_name] = YOUR_VALUE;