我正在使用 jQueryUI 小部件工厂,我正在尝试检索我在 html 文件中设置的选项。
这是小部件代码(form.js):
(function ($, undefined) {
$.widget('ulti.uiForm', {
//#region options
options: {
url: "",
thankYou: true,//Enable thank you page
thankYouPage:null,
selector: 'form',
posted: null, // Callback for ajax done method.
//#region leadTemplate
leadTemplate : {
},
//#endregion leadTemplate
},
//#endregion options
_create: function () {
this.element.addClass("ulti-uiForm");
},
_init: function () {
// Load/copy lead template to element instance data...
this.element.data('lead', this.options.leadTemplate);
this._appendQSParms(this.element.data('lead'));
},
destroy: function () {
// Restore element to pre-uiForm create state...
this.element.removeClass('ulti-uiForm');
this._destroy();
},
// Very basic diagnosic method to check that all minimum requirements are populated on the object.
// TODO EXPOSE .done as a posted callback in options.
post: function () {
var $ajaxResult;
var self = this;
$.ajax({
type: 'POST',
url: this.options.url,
data: JSON.stringify(this.element.data('lead')),
dataType: "json",
contentType: "application/json; charset=UTF-8",
accept: "application/json",
beforeSend: function () {
}
})
// callback handler on success - populate lead object with returned data.
.done(function (response, textStatus, jqXHR) {
self.element.data('lead', response);
self._trigger("posted", self.element.data('lead'));
if (options.thankYou === true) {
window.location.replace(options.thankYouPage);
}
})
// callback handler that will be called on failure
.fail(function (jqXHR, textStatus, errorThrown) { //.fail(function () {
// log the error to the console
console.error("Error POSTing gateway object. Status:" + textStatus + " Error: " + errorThrown);
})
.always(function (jqXHR, textStatus, errorThrown) {
});
return self; // support chaining...
},
_setOption: function (key, value) {
this._super(key, value);
}
});
})(jQuery);
html中的JS:
var uiForm;
uiForm = $('#stepform').uiForm({
options: {
url: 'http://someurl.cloudapp.net/api/leads',
thankYouPage:'thankyou.html',
posted: function (evt) {
//notEqual(uiForm.data('lead').SessionId, null, "Post operation succeeded lead sessionId: " + uiForm.data('lead').SessionId);
//start();
}
}
});
$("#btnSubmit").click(function () {
//post the form
uiForm.uiForm('post');
});
我只是想从 html 文件中的 js 发送thankYouPage 选项到 form.js 并将该选项应用于“完成”回调:
if (this.options.thankYou === true) {
window.location.replace(options.thankYouPage);
}
非常感谢您的洞察力。