我需要通過單擊一個按鈕發送兩個表單。
此腳本適用於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>
我與鉻的控制檯檢查,其結果是:
Uncaught ReferenceError: isset is not defined (index):603submitForms (index):603onclick (index):446
未捕獲的ReferenceError:未定義validateForm(指數):421onsubmit(指數):421
請檢查該鏈接http://stackoverflow.com/questions/9096515/如何提交2表單與單一提交按鈕 – mjdevloper 2014-10-06 09:33:12
看看你的'如果'條件。條件之間應該有'&&'而不是','。 'form2.User_Email.value.length == 0 && form2.Friend_Email.value.length == 0'等等。現在「如果」的決定是基於最後的條件。 – Regent 2014-10-06 09:36:09
好吧我改變了代碼,但現在不起作用,只啓動一個窗體(form2)第一個被忽略 – Alberto 2014-10-06 10:21:57