admin管理员组

文章数量:1391964

Here is a simple form:

<form action="hello">

</form>

If I use

var action = $('form').attr('action');

I get the correct value, "hello". But if I use

var action = $('form').prop('action');

I get

http://localhost/hello

What's up with that?

I read that I should use prop() instead of of attr() but here it returns incorrect value

Here is a simple form:

<form action="hello">

</form>

If I use

var action = $('form').attr('action');

I get the correct value, "hello". But if I use

var action = $('form').prop('action');

I get

http://localhost/hello

What's up with that?

I read that I should use prop() instead of of attr() but here it returns incorrect value

Share Improve this question asked Dec 11, 2014 at 7:26 Anna K.Anna K. 2,0056 gold badges28 silver badges40 bronze badges 3
  • how about var action = document.form.action? – Igor Savinkin Commented Dec 11, 2014 at 7:31
  • @IgorSavinkin document.forms[0].action then. And it will return absolute path. – Regent Commented Dec 11, 2014 at 7:33
  • Who told you to use prop and why? – David Knipe Commented Dec 11, 2014 at 8:20
Add a ment  | 

1 Answer 1

Reset to default 10

When you say prop, it will get the absolute path of the target resource but .attr() will read the attribute value as it is.

Since you have used relative path in the action, prop will use the path of the current page to construct the actual url to which the form will be submitted.

本文标签: javascriptForm action value difference in prop and attrStack Overflow