cwo*_*cwo 0 javascript jquery scope
我有以下Javascript代码,它创建一个具有click事件的按钮元素.
function Button(id, url, blockMsg){
var id = id;
var url = url;
var blockMsg = blockMsg;
var message;
this.getId = function(){
return id;
};
this.getMessage = function(){
return message;
};
block = function(msg){
$.blockUI({
message: msg
});
};
unblock = function(){
$.unblockUI();
};
showErrors = function(){
console.log('errors');
}
$(id).bind('click', function(){
$.ajax({
url: url,
type: 'POST',
beforeSend: function(){
block(blockMsg);
},
error: function(response){
message = $.parseJSON(response);
message.action();
unblock();
console.log(action);
},
success: function(response){
message = $.parseJSON(response);
[message.action]();
unblock();
console.log(action);
}
});
});
};
$(document).ready(function(){
var buttonRegister = new Button('#btnCompanyRegister', '/company/register/', 'register ...');
});
Run Code Online (Sandbox Code Playgroud)
当我点击按钮时,一切正常,我的PHP脚本返回
json_encode(array('action' => 'showErrors'));
Run Code Online (Sandbox Code Playgroud)
在FireBug中,我可以看到错误:["showErrors"]不是函数
我究竟做错了什么?为什么没有指定功能?我有范围问题吗?
谢谢您的帮助.
而不是[message.action]();使用window[message.action]();.
message.action是字符串"showErrors" - 这不是一个函数.您可以showErrors从window对象获取全局函数.
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |