我需要点击一个按钮发送两个表格 此脚本适用于Firefox和Internet Explorer,但不适用于Chrome和Safari。
<form action="linkweb" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
</form>
<form action="unoamoltissl.php" method="post" id="form2" style="width:450px;margin-left:-10px;">
</form>
<input type="button" value="" onclick="submitForms();" style="background-image:url('images/sharenow.png');width:200px;height:60px;background-repeat:no-repeat;border:none;margin-left:35px;"/>
<script type="text/javascript">
submitForms = function() {
if (form2.User_Email.value.length == 0, form2.Friend_Email.value.length == 0, form2.Friend_Email_Terzo.value.length == 0)
{
document.getElementById('errfn').innerHTML = "* Please complete all the 3 fields to share.";
}
else if (form2.User_Email.value.length != 0, form2.Friend_Email.value.length != 0, form2.Friend_Email_Terzo.value.length != 0)
{
document.getElementById("mc-embedded-subscribe-form").submit();
document.getElementById("form2").submit();
}
return false;
}
</script>
我用这个更新了代码,但现在只处理第二种形式而不是第一种形式:
<script type="text/javascript">
$(document).ready(function() {
$("#subbut").click(function() {
$.post($("#mc-embedded-subscribe-form").attr("action"), $("#mc-embedded-subscribe-form").serialize(),
function() {
$.post($("#form2").attr("action"), $("#form2").serialize(),
function() {
location.href = 'index2.html';
});
});
});
});
<button id="subbut" onclick="submitForms();" style="background-image:url('images/sharenow.png');width:200px;height:60px;background-repeat:no-repeat;border:none;margin-left:35px;"/></button>
我检查了chrome的控制台,结果是:
Uncaught ReferenceError: isset is not defined (index):603submitForms (index):603onclick (index):446
未捕获的ReferenceError:未定义validateForm(索引):421onsubmit(index):421
答案 0 :(得分:0)
如果第一个表单发布错误会怎样?你只有成功的回调..
也许值得使用$ ajax,这样你就可以定义(并检测)第一篇文章失败时会发生什么......至少在诊断问题时,无论如何:
$。AJAX({ 类型:“POST”, url:url, 数据:数据, 成功:成功, 错误:错误, dataType:dataType });
答案 1 :(得分:0)
我用这段代码解决了这个问题,希望可以帮助其他人
<form action="test2.php" method="post" id="form2" name="f2" style="width:450px;margin-left:-10px;"></form>
<form action="test1.php" method="post" id="form1" name="f1" style="width:450px;margin-left:-10px;"></form>
<input type="button" onClick="runscript();"/>
<script type="text/javascript">
function runscript() {
document.forms.f1.submit();
window.setTimeout(function() {
document.forms.f2.submit();
}, 100);
}
</script>
问候
阿尔贝托