jquery to goto a url on form submit with field values added to the url string

十年热恋 提交于 2019-12-03 16:38:09
Alex Sexton

jQuery serialize might help in a simple case like the one you describe:

$('#form').submit(function(){
  location.href = 'index.html'+$(this).serialize();
});

You have it right, almost.

var firstName = $("#firstName").val();
var surname = $("#surname").val();

location.href = "index.html?firstName=" + firstName + "&surname=" + surname

Of course, you could make a function that accepts a list of key/value pairs in an object and make this generic, but you get the idea.

$('#submit').click( function() { 
location.href = "";
return false;
} );
tbwcf

thank you both for your help on this...

I'm not sure if I was doing something wrong with your methods or if I hadn't described what I needed to do well enough...

The solution in the end for me was this...

first file contains a a form with method="get"

< form method="get" action="getParams.html">
First Name: < input type="text" name="firstName" id="firstName" />
Surname: < input type="text" name="surname" id="surname" />
< input type="submit" id="submit" value="submit"/>
< /form>

this directs to get params and as the method=get the url ends with getParams.html?firstname=""&surname=""

Then in the getParams.html page this is simply used for the formatting (as i need it to be) from the url and redirect...

I used attached jquery followed by this plugin http : // projects.allmarkedup.com/jquery_url_parser/

with the js:

var firstName = jQuery.url.param("firstName");
var surname = jQuery.url.param("surname");
location.href = 'http://myOnlineMag/issue01.html?params=' + firstName + '*' + surname; 

pulls the values I want form the url as vars then attaches them to the end of my url. (I needed these in this specific format - can't share a link until the projects live but I can post next week if you would like.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!