Javascript 一个脚本中有两个post目标,表单操作标记为空?
以下是简单的表格:Javascript 一个脚本中有两个post目标,表单操作标记为空?,javascript,jquery,html,Javascript,Jquery,Html,以下是简单的表格: <form id="contactform" method="POST" action="" > <div class="form-group"> <input type="text" class="form-control" id='first_name' name='first_name' > <button type="submit" class="btn bt
<form id="contactform" method="POST" action="" >
<div class="form-group">
<input type="text" class="form-control" id='first_name' name='first_name' >
<button type="submit" class="btn btn-success btn-lg " name="form_subbmit">Send</button>
</form>
发送
和子BMIT的myscript:
<script>
// Attach a submit handler to the form
$( "#contactform").submit(function( event ) {
var $form = $( this );
first_name=$form.find( "input[name='first_name']" ).val();
$.post( "http://post1.com", { first_name: first_name } );
$.post( "http://post2.com", {first_name: first_name } );
});
</script>
//将提交处理程序附加到表单
$(“#联系人表单”)。提交(功能(事件){
var$form=$(此);
first_name=$form.find(“输入[name='first_name']”).val();
$.post(”http://post1.com“,{名字:名字});
$.post(”http://post2.com“,{名字:名字});
});
现在,若我将两个post目的地中的一个放在表单的作用中,一切都很好。如果使用这样的代码,则根本不需要提交
如何将数据发送到邮寄目的地,并在表格空白的情况下采取措施
Tnx请尝试以下代码:
<script>
function submitWithPost(){
var $form = $( this );
first_name=$form.find( "input[name='first_name']" ).val();
$.post( "http://post1.com", { first_name: first_name } );
$.post( "http://post2.com", {first_name: first_name } );
}
</script>
<form onsubmit="return submitWithPost();" >
函数submitWithPost(){
var$form=$(此);
first_name=$form.find(“输入[name='first_name']”).val();
$.post(”http://post1.com“,{名字:名字});
$.post(”http://post2.com“,{名字:名字});
}
首先放置事件预防默认值:
$("#contactform").submit(function( event ) {
event.preventDefault();
...
});
完成后,必须按如下方式清理输入文本字段:
$.post( "http://post1.com", { first_name: first_name })
.done(function(data) {
$form.find("input[name='first_name']").val('');
});
使用
event.preventDefault()
在submit
Tnx的第一行阻止表单的默认事件。告诉我,我是这样做的$(“#contactform”).submit(函数(事件){event.preventDefault();在函数内部..获取错误:500内部服务器错误,仅使用firebugInternal服务器错误进行测试意味着这是服务器端错误!!