我确实尝试过这样做(使用 jQuery):
type = (blabla == blublu)? 'category':'regular';
(type+'Name').val('...');
这是控制台响应:
未捕获的类型错误:对象类别名称没有方法“val”
虽然它们确实存在...
那我怎样才能让它工作?
编辑:
这是完整的功能:
var type = (1 == 1)?'something':'blabla';
var somethingName = $('#somethingName');
var blablaName = $('#blablaName');
more variables like this..
function itemMode(mode, inputValue){
inputValue = (typeof inputValue !== 'undefined')? inputValue:'';
var functionName = null;
switch(mode){
case 'open':
btnValue = 'something';
functionName = editCategory; // this need to be from type to
openItem = true;
break;
case 'close':
btnValue = 'something';
functionName = addCategory; // this need to be from type to
openItem = false;
break;
default:
return false;
}
(type+'Name').val(inputValue);
$('#btn'+type.capitalize()).off('click').val(btnValue).click(functionName);
(type+'Name').off('keypress').keypress(function(e){enterPress(e,functionName);});
}
我尝试 (type+'Name') 创建对现有变量或对象的引用。