假设在html页面中具有id"serialform"的表单.
以下JavaScript按预期工作.
$( "#serialform" ).submit(function( event ) {
var action = $( "#serialform" ).attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我替换函数的第一行使用"this":
$( "#serialform" ).submit(function( event ) {
var action = this.attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
然后表单提交而不是简单地返回action属性. this表示元素(根据萤火虫).
我原以为这this相当于$( "#serialform" ).无论如何,我想获取正在提交的表单的action属性.
我正在使用jQuery 2.0.3.