它们有什么区别,我如何知道何时使用哪个?
$.post($(this).attr('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
$.post($(this).prop('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
$.post($(this).closest("form").prop('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
最佳答案
在这种情况下,第一个和第二个是相同的。必须从表单事件处理程序调用这些函数。除了 $(this).prop/attr()
,也可以使用 $(this)[0].action
和 this.action
使用过。
第三个方法查找最近的表单元素,并检索表单的 action
属性。该方法对于非形式上下文很有用,例如来自 button
元素。
关于javascript - 这些 POST 命令有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7471860/