我想通過一個jquery插件來傳遞一個函數作爲選項,我正在構建自己。創建你自己的jQuery插件 - 功能問題
目前使用下列選項初始化我的插件時:
$.fitAjaxSubmit({
formId: "new_team",
postUrl: "/teams/create.json",
redirectUrl: "/teams/",
saveBoxText: "Saving Team...",
beforeSubmitFn: function(){ alert('Boo!'); }
});
在我的插件,我試圖調用beforeSubmitFn
像這樣(我還使用了Ajax表單插件):
(function($){
$.fn.fitAjaxSubmit = function(options) {
var defaults = {
formId: "formId",
postUrl: "postUrl",
redirectUrl: "redirectUrl",
saveBtnId: "save_button",
saveBtnEnableText: "Save",
saveBtnDisableText: "Saving...",
saveBoxId: "saving-box",
saveBoxText: "Saving...",
errorMsgId: "error_messages",
beforeSubmitFn: function(formData, jqForm, options) {},
successFn: function(response) {
if(response.success == true) {
window.location = options.redirectUrl + response.object_id;
} else {
$('#'+options.saveBtnId).removeAttr('disabled').val(options.saveBtnEnableText);
$('#'+options.saveBoxId).text(options.saveBoxText).fadeOut(300);
$('#'+options.errorMsgId).html(errorMessages(response)).hide().fadeIn(300);
window.scrollTo(0,0);
}
}
};
var options = $.extend(defaults, options);
return this.each(function() {
$('#'+options.saveBtnId).click(function() {
$('#'+options.saveBtnId).attr('disabled', 'disabled').val(options.saveBtnDisableText);
$('#'+options.saveBoxId).text(options.saveBoxText).fadeIn(200);
$('#' + options.formId).ajaxForm({
url: options.postUrl,
type: "POST",
dataType: "json",
beforeSubmit: options.beforeSubmitFn,
success: options.successFn
})
});
});
};
})(jQuery);
由於某些原因,beforeSubmitFn在Firefox中工作,但在Internet Explorer,Safari和Chrome中根本不起作用!
有什麼我在這裏失蹤?
在此先感謝!
在您訪問它時,可能選項已超出範圍。剩餘的選項值是否有效?你能給我們多一點代碼嗎? – 2009-09-23 09:01:42
只需添加更多代碼!多謝你們! – schone 2009-09-23 09:06:07
它似乎與此行隔離$('#'+ options.saveBtnId).attr('disabled','disabled')。val(options.saveBtnDisableText); - 不能解決爲什麼,但! – schone 2009-09-23 09:23:53