如何使用JavaScript提交表单并重定向到URL?

如何使用JavaScript提交表单并重定向到URL?

name :
age:

请看这个链接中的图片:

http://sofzh.miximages.com/javascript/zCzQIH.jpg

按下提交按钮后,我想将页面重定向到www.example.com/aaa/robert/21

我怎样才能做到这一点 ?

用html代码试试这个

 
name :
age:

和javascript

  

别忘了添加jQuery库

也许是这样的:

HTML:

 
name :
age:

注意:不推荐使用form标签中的name属性,使用id属性。 Inputselecttextarea标签也应该有id

JS:

 $('.js-redirect').on('submit', function() { var newUrl = this.action.replace('.php', '/' + $('#value_1').val() + '/' + $('#value_2').val()); this.action = newUrl; }) 

在php文件中设置标头

 $dynamic = $_GET['value_1']; $id =$_GET['value_2']; header('location:www.example.com/aaa/'.$dynamic.'/'.$id); 

单击按钮尝试此function

 function myFunction() { document.getElementById("myForm").submit(); }