admin管理员组文章数量:1302381
Assume a form with id "serialform" in an html page.
The following JavaScript works as expected.
$( "#serialform" ).submit(function( event ) {
var action = $( "#serialform" ).attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
However, if I replace the first line of the function to use "this":
$( "#serialform" ).submit(function( event ) {
var action = this.attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
Then the form submits instead of simply returning the action attribute. this
refers to the form element (according to firebug).
I would have thought that this
would be equivalent to $( "#serialform" )
. In any event, I want to get the action attribute of the form that is being submitted.
I'm using jQuery 2.0.3.
Assume a form with id "serialform" in an html page.
The following JavaScript works as expected.
$( "#serialform" ).submit(function( event ) {
var action = $( "#serialform" ).attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
However, if I replace the first line of the function to use "this":
$( "#serialform" ).submit(function( event ) {
var action = this.attr("action");
var serial = $("#serial").val()
...
return event.preventDefault();
});
Then the form submits instead of simply returning the action attribute. this
refers to the form element (according to firebug).
I would have thought that this
would be equivalent to $( "#serialform" )
. In any event, I want to get the action attribute of the form that is being submitted.
I'm using jQuery 2.0.3.
Share Improve this question edited Dec 9, 2013 at 17:09 Jonathan de M. 9,8288 gold badges49 silver badges72 bronze badges asked Dec 9, 2013 at 16:55 RickRick 3625 silver badges16 bronze badges 4-
1
replace
this.attr
with$(this).attr
– MackieeE Commented Dec 9, 2013 at 16:56 -
Try changing
this
to$(this)
. – j08691 Commented Dec 9, 2013 at 16:56 -
Because you get the error "TypeError: undefined is not a function" or something like that. Learn how to debug JavaScript. And please read the jQuery tutorial (learn.jquery./events/event-basics). It explains what happens inside an event handler and what
this
refers to. – Felix Kling Commented Dec 9, 2013 at 16:57 -
2
Avoid issues like this by moving
event.preventDefault();
to the first line of your event handler. By doing so it would have not submitted the form, and instead, shown an error in your console. – Kevin B Commented Dec 9, 2013 at 16:58
2 Answers
Reset to default 5this
is a reference to the DOM node, not the wrapped jQuery node. This DOM node doesn't implement the .attr
method, so it throws an error.
You need to wrap the DOM node in a jQuery object before you call a jQuery method, like so: $(this).attr('action')
.
It submits because this.attr
raises an error (something like "Object whatever has no method 'attr'), then further code doesn't run and the default behavior is not prevented. Check your console.
Inside a jQuery event handler, this
is the DOM element that triggered the event (the form, in this case). It's not a jQuery object, so it doesn't have an attr
method.
本文标签: javascriptjquery thisattr(quotactionquot) submits formStack Overflow
版权声明:本文标题:javascript - jquery this.attr("action") submits form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741707075a2393624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论