jQuery jquery转到表单提交上的网址,并将字段值添加到网址字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1610214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:49:44  来源:igfitidea点击:

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

jquerystringurlparams

提问by tbwcf

I'm fairly new to jquery and I think this is simple but I'm struggling and google doesn't seem to be helping....

我对 jquery 相当陌生,我认为这很简单,但我很挣扎,谷歌似乎没有帮助....

What I have is 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 when submit is clicked I want to goto a url but then add the froms values to the url string (but not using the form method=get) basicly I want this to happen

现在,当单击提交时,我想转到一个 url,然后将 froms 值添加到 url 字符串中(但不使用表单 method=get)基本上我希望这发生

on click of submit goto http://myurl.com/index.html?params=firstNamevalue*surnamevalue

单击提交转到 http://myurl.com/index.html?params=firstNamevalue*surnamevalue

I've been hacking about with the jquery but where I'm upto is something like this...

我一直在用 jquery 进行黑客攻击,但我喜欢的地方是这样的......

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

hopefully I've been farily clear - I'd greatly appreciate any help!

希望我已经清楚了 - 我非常感谢任何帮助!

Andy

安迪

采纳答案by 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"

第一个文件包含一个表单 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=""

这指示获取参数,并且作为 method=get url 以 getParams.html?firstname=""&surname=""

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

然后在getParams.html页面中,这仅用于从 url 和重定向进行格式化(如我所需要的那样)...

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

我使用了附加的 jquery,然后是这个插件 http : // projects.allmarkedup.com/jquery_url_parser/

with the js:

与 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.)

将我想要的值从 url 中提取为 vars,然后将它们附加到我的 url 的末尾。(我需要这种特定格式的这些 - 在项目上线之前无法共享链接,但如果您愿意,我可以在下周发布。)

回答by Alex Sexton

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

jQuery serialize可能会在您描述的简单情况下有所帮助:

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

回答by Stefan Kendall

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;
} );