提交给多个网址 [英] submit to multiple urls

查看:208
本文介绍了提交给多个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个字段的html表单:姓名和电子邮件。
我需要从相同的提交按钮提交表单值到2个不同的URL。我曾尝试使用jQuery,序列化和发布,但我不清楚如何完成此操作。我不能在这个实例中使用PHP。

 <!doctype> 
< html>
< head>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js>< / script>

< / head>
< body>

< form action =method =postname =myformid =myform>

< input type =textvalue =name/>
< input type =emailvalue =email/>
< input type =buttonid =submitvalue =submit/>
< / form>

< script type =text / javascript>
$(document).ready(function(){
var url1 =some-url.com;
var url2 =some-other-url;
$ (#submit)。click(function(){
$ .post(url1,$(form#myform)。serialize());
$ .post(url2 ,$('formmy#myform')。serialize());
})
})
< / script>
< / body>


解决方案

如果您使用jquery:


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$。
//其他
alert(Handler for .submit()called。);
event.preventDefault(); //这样它就不会再次取消
});

submitToURL1 可能类似于:

.serialize()是一个jquery函数它将表单中的元素很好地排列起来,例如,可以将它们作为参数发送给post方法。 https://api.jquery.com/serialize/



更多信息: https://api.jquery.com/submit/


I have an html form with 2 fields: name and email. I need to submit the form values to 2 different urls from the same submit button. I have tried using jquery, serialize and post but I am not clear how to accomplish this. I cannot use PHP in this instance.

<!doctype>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>
<body>

<form action="" method="post" name="myform" id="myform">

<input type="text" value="name" />
<input type="email" value="email" />
<input type="button" id="submit" value="submit" />
</form>

<script type="text/javascript">
$(document).ready(function(){
    var url1 = "some-url.com";
    var url2 = "some-other-url";
   $("#submit").click(function() {
        $.post("url1", $("form#myform").serialize());
    $.post("url2", $('formmy#myform').serialize());
    })
})
</script>
</body>

解决方案

If you're using jquery:

$( "form" ).submit(function( event ) {
  submitToURL1();
  submitToURL2();
  //something else
  alert( "Handler for .submit() called." );
  event.preventDefault(); //so that it doesn't get suimitted again
});

submitToURL1 could be something like:

$.post("url1", data : $("form").serialize());

.serialize() is a jquery function that takes the elements in a form and arranges them nicely so you can send them as params to a post method, for instance. https://api.jquery.com/serialize/

More information here: https://api.jquery.com/submit/

这篇关于提交给多个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆