假设问题是“如何在提交事件之后向单元添加点击处理程序?”那么答案可能是您的成功回调有几个语法错误,您的缩进表明您以不正确的顺序管理DOM。为了说明这里是一些注释代码:
$('form.ajax').on('submit', function(e) {
// Prevent an actual submission in favor of AJAX
e.preventDefault();
// Spawn an AJAX request to http://ecample.com/api/form_validator
$.ajax({url: 'http://example.com/api/form_validator'})
// Once we have a success response *then* do the following.
.then(function(response) {
// Use the response from the server to iterate of the keys array.
$.each(response.keys, function(i, key) {
// add the missing class to the element that matches the key.
$('#element-' + key).addClass('missed');
});
});
});
// Assign a click event to the entire form but filter it by 'input.missing'
// this way whe an input does have the missingf class the click will happen
// otherwise it is ignored. This is call event delegation.
$('form.ajax').on('click', 'input.missed', function(e) {
alert('Clicked');
});
这就是呼叫event delegation。
'成功'回调缺少右括号 – DontVoteMeDown 2015-03-02 18:58:37
...和缺少'});'在阿贾克斯调用 – acbabis 2015-03-02 19:00:23
而且您不取消提交操作。 – epascarello 2015-03-02 19:08:09