我知道函數可以存儲在特定事件(例如onmouseover)的html屬性上,但是任何屬性都可以引用一個函數或只是特殊的屬性?功能名稱作爲HTML屬性
我想自動根據表單屬性連線ajax成功和失敗回調[例如,的onSuccess = 「警報( 'whoohoo');」和onfail =「alert('你吸');」]。是否有這樣的可能,或者我必須將函數存儲在已知事件中,並在失敗或成功時觸發事件?
我想與社區分享結果代碼與社會的好處。不幸的是我不能使用data- *作爲屬性名稱,因爲' - '對於屬性名稱不是有效字符,而是使用MVC和匿名類型創建表單。
// provide the ability to manually setup a form for ajax processing.
// target are the forms to be wired
// success is the callback function
function AjaxifyForms(target, success, fail) {
$(target).each(function() { // Wierdness if each isn't used with validation
$(this).validate({
submitHandler: function(form) {
$.ajax({ //create an AJAX request
type: "POST", //use POST (we could also load this from the form if wanted to)
url: $(form).attr("action"), //get the URL from the form
data: $(form).serialize(),
datatype: "json",
success: function(data) {
if (success != undefined && success != null) success(data);
else if ($(form).attr("ajaxSuccssCallback") != undefined && $(form).attr("ajaxSuccssCallback") != null)
window[$(form).attr("ajaxSuccssCallback")](data);
else alert("Success");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (faile != undefined && fail != null) fail(data);
else if ($(form).attr("ajaxFailCallback") != undefined && $(form).attr("ajaxFailCallback") != null)
window[$(form).attr("ajaxFailCallback")](data);
else alert("Epic Fail");
}
});
return false; //don't forget to cancel the event so we don't submit the page!
}
});
});
}
你可以做一些瀏覽器測試,看看有什麼作品,什麼不可以。這就是我要做的。 – zzzzBov 2010-12-16 17:46:51
我想爲序列化做同樣的事情,但它看起來像你沒有找到一種方法來做到這一點, – Michael 2014-03-13 05:28:37