我知道函数可以存储在特定事件(例如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