Dynamic adding action url to form with parameter not setting to url

时间:2015-07-28 16:02:39

标签: jquery

I have form and I want to put dynamic action url with parameter using jquery

I tried this

 $('form#myform').attr('action','mynewurl.php?id='+id);

and it gives me like this

action="mynewurl.php?id=40"

when I submitted the form the parameter is not set in the url

http://localhost/mysite/mynewurl.php

I don't know why the parameter is missing in the url when submitted the form

Thank you in advance.

2 个答案:

答案 0 :(得分:0)

Use the complete address the second parameter to attr() function is string containing value .Action can navigate use to other server too that why its not overheading server name with url. In case of it will work because if has no server info by default i will navegite to current server

 $('form#myform').attr('action','http://localhost/mysite/mynewurl.php?id='+id);

答案 1 :(得分:0)

Try submitting this form

<form action="/something.php?id=40">
  <input type="submit" value="submit">
</form>

You will see that since the method is GET, all the params are replaced with the form fields' values. If there are no fields it will default to /something.php and lose the id param.

You will need to either change the form method to POST or add a hidden field to the form and change its value.

<form method="get" action="/something.php">
  <input type="hidden" value="1" name="id">
  <input type="submit" value="submit">
</form>