开发者

What is the difference of these POST commands?

What is the difference of these, and how do I know when to use which?

$.post($(this).attr('action'), $(this).serialize(), function(response) {
    // do something here on success
}, 'json');

$.post($(this).prop('action'), $(this).serializ开发者_高级运维e(), function(response) {
    // do something here on success
}, 'json');

$.post($(this).closest("form").prop('action'), $(this).serialize(), function(response) {
    // do something here on success
}, 'json');


The first and second are identical, in this case. These functions has to be called from a form event handler. Instead of $(this).prop/attr(), $(this)[0].action and this.action can also be used.

The third method looks for the nearest form element, and retrieve the action attribute of the form. This method would be useful from a non-form context, e.g. from a button element.


The first and second would be appropriate for a "submit" event handler associated with a <form> element. The second is probably better, but in actual use they're almost the same.

The third would be useful as a "click" handler on a <button> inside a <form>, or some similar situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜