Jquery to goto url on form submit with field values ​​added to url string

I am new to jquery and I think it is simple, but I am struggling, and google does not seem to help ...

I have a basic form ...

<form>
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>

Now that the submit button is clicked, I want to go to the URL, but then add the froms values ​​to the url string (but not using the form = get method). I want this to happen

when you click submit http://myurl.com/index.html?params=firstNamevalue*surnamevalue

I hacked using jquery, but where I go up is something like this ...

< script src="http://www.google.com/jsapi" type="text/javascript">< /script>
< script type="text/javascript">
    google.load("jquery", "1.3.1");
</script>

< script type="text/javascript">

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


$('#submit').click(function(){
  window.location = ???;
}); 
< /script>

I hope I was very clear - I would really appreciate any help!

Andy

+3
source share
3 answers

Thank you for your help in this ...

, - , , ...

...

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>

= URL getParams.html?firstname=""&surname=""

getParams.html ( ) URL- ...

jquery, http : // projects.allmarkedup.com/jquery_url_parser/

js:

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

, url vars, URL-. ( - , , , .)

0

jQuery serialize , :

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

, .

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

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

Of course, you can make a function that takes a list of key / value pairs in an object and does this in common, but you get the idea.

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

Source: https://habr.com/ru/post/1720841/


All Articles