多动作形状发送 ajax

我需要将数据写入两个表 api 用表格。 一个动作被记录在一个表中,另一个动作录制到另一个表中。 但是,由于它在一个页面上,我只需要一个发送按钮,而且我知道不可能有两个不同的动作。 但与此同时,我知道我需要使用两种不同的形式 AJAX, 对于我的第一个形式,我不能做任何事情,但我想知道我是如何使用的 AJAX 使用几个行动,以适应两个不同的表格中的以下形式。 所以谁能告诉我我怎能呢?


<div id="login">
<div id="triangle"></div>
<h1>Opt in</h1>
<form action=" method="post">
<input name="user_email" placeholder="Email" type="user_email"/>
<input name="user_name" placeholder="Your name" type="user_name"/>
<input name="user_skills" placeholder="Skills" type="user_skills"/>
<input name="user_bio" placeholder="Biography" type="user_bio"/>
<input name="user_other" placeholder="Other Information" type="user_other"/>
</form>
</div>
<div id="whitelist">
<h1>Register</h1>
<form action=" method="post">
<input name="device_id" placeholder="Device ID" type="device_id"/>
<input name="device_description" placeholder="Describe your Device" type="device_description"/>
<input name="device_name" placeholder="Type of Device" type="device_name"/>
<input type="submit" value="Submit"/>
</form>
</div>`
已邀请:

涵秋

赞同来自:

好的,会有很多工作,我会尝试解释它,以便你很容易理解:

HTML

:

拨打电话 ajax 您不需要表单,因此您还可以删除它或至少从中删除属性
action


method

:


<div id="login">
<div id="triangle"></div>
<h1>Opt in</h1>
<form>
<input name="user_email" placeholder="Email" type="user_email"/>
<input name="user_name" placeholder="Your name" type="user_name"/>
<input name="user_skills" placeholder="Skills" type="user_skills"/>
<input name="user_bio" placeholder="Biography" type="user_bio"/>
<input name="user_other" placeholder="Other Information" type="user_other"/>
</form>
</div>
<div id="whitelist">
<h1>Register</h1>
<form>
<input name="device_id" placeholder="Device ID" type="device_id"/>
<input name="device_description" placeholder="Describe your Device" type="device_description"/>
<input name="device_name" placeholder="Type of Device" type="device_name"/>
<input type="submit" value="Submit"/>
</form>
</div>


jQuery

:

在我们去代码之前 jQuery, 我建议你读一点
http://api.jquery.com/jquery.ajax/
.

我们现在转向代码最有趣的部分:

在事件中 click 纽扣 submit 我们称之为 ajax 并将值发送到相应的表格,如下所定义:


$/document/.on/'click','input[type=submit]',function/e/{
e.preventDefault//;
$.ajax/{
type:'post',
url:",
data:$/'form:eq/0/'/.serialize//,
success:function/resp/{
alert/'first table sent'/;
},
fail:function/resp/{
alert/'couldn\'t send the first table'/;
}
}/;
$.ajax/{
type:'post',
url:",
data:$/'form:eq/1/'/.serialize//,
success:function/resp/{
alert/'second table sent'/;
},
fail:function/resp/{
alert/'couldn\'t send the second table'/;
}
}/;
}/;


NOTE

:

它可能根本无法工作,因为可能有很多机会犯错误,这就是为什么我为你读到的原因 jQuery-AJAX 提前。 虽然您可以随时询问任何后续问题

StackOverflow

.
</ipaddress></ipaddress>

要回复问题请先登录注册