2017-02-15 51 views
2

我想請求幫助將複選框合併到一個字段中的函數。有問題Combine checkbox values into string before submitting form我找到了一個,但我希望它開始onsubmit與另一個函數,檢查表單是否填寫正確。javascript - 將複選框合併到表單中的一個字段中

形式:

<form id="formularz_wspolpraca" name="Zapis na poradnik" method="post" target="_top" onsubmit="return SprawdzFormularz(this) && mergeFunction(this)"> 
    <input type="text" id="email" name="email"/> 
    <input type="text" id="imie" name="imie"/> 
    <input type="text" id="nazwisko" name="nazwisko"/> 
    <input type="text" maxlength="12" size="12" id="pole_1" name="pole_1"/> 
    <input class="checkbox_wspolpraca" type="Checkbox" name="pole_3a" value="polecajacy"> 
    <input class="checkbox_wspolpraca" type="Checkbox" name="pole_3b" value="projektant"> 
    <input class="checkbox_wspolpraca" type="Checkbox" name="pole_3c" value="instalator"> 
    <input class="checkbox_wspolpraca" type="Checkbox" name="pole_3d" value="ekspert"> 
    <input type="hidden" name="pole_3" id="pole_3"> 
    <input id="pp" type="checkbox" name="pp" checked=""/> 
    <input type="submit" value="Wyślij"> 
</form> 

合併功能:

function mergeFuntion(event) { 
    event.preventDefault(); 
    var boxes = document.getElementsByClassName('checkbox_wspolpraca'); 
    var checked = []; 
    for (var i = 0; boxes[i]; ++i) { 
     if (boxes[i].checked) { 
     checked.push(boxes[i].value); 
     } 
    } 
    var checkedStr = checked.join(' '); 
    document.getElementById('pole_3').value = checkedStr; 
    return true; 
} 

檢查功能:

function SprawdzFormularz(f) { 
    if (f.email.value == "") { 
     alert("Nie poda\u0142e\u015b/a\u015b adresu e-mail."); 
     return false; 
    } 
    if (((f.email.value.indexOf("@", 1)) == -1) || (f.email.value.indexOf(".", 1)) == -1) { 
     alert("Poda\u0142e\u015b/a\u015b b\u0142\u0119dny adres e-mail."); 
     return false; 
    } 
    if (f.imie.value == "") { 
     alert("Wype\u0142nij pole Imi\u0119. "); 
     return false; 
    } 
    if (f.nazwisko.value == "") { 
     alert("Wype\u0142nij pole Nazwisko. "); 
     return false; 
    } 
    if (f.pole_1.value == "") { 
     alert("Wype\u0142nij pole Nr telefonu. "); 
     return false; 
    } 
    if ((f.pole_3a.checked == false) && (f.pole_3b.checked == false) && (f.pole_3c.checked == false) && (f.pole_3d.checked == false)) { 
     alert("Wybierz zakres wsp\u00f3\u0142pracy"); 
     return false; 
    } 
    if (f.pp.checked == false) { 
     alert("Musisz zgodzi\u0107 si\u0119 z Polityk\u0105 Prywatno\u015bci."); 
     return false; 
    } 
    return true; 
} 

檢查功能工作沒有問題,但我不能讓合併一個工作湖有人可以指出我在做什麼錯誤的合併功能?我對JavaScript很新,所以可能會出現一些菜鳥的錯誤。提前致謝。

回答

1

在onsubmit中,您首先運行SprawdzFormularz,如果所有檢查都通過,則返回true。這意味着它將在合併函數運行之前提交表單。

您需要在返回true之前在檢查函數內部運行合併函數,以便在合併字符串並設置必要的值之前,表單不會提交。

function SprawdzFormularz(f) { 

    // .... 


    var boxes = document.getElementsByClassName('checkbox_wspolpraca'); 
    var checked = []; 
    for (var i = 0; boxes[i]; ++i) { 
     if (boxes[i].checked) { 
      checked.push(boxes[i].value); 
     } 
    } 
    var checkedStr = checked.join(' '); 
    document.getElementById('pole_3').value = checkedStr; 

    return true; 
} 
+0

工程就像一個魅力。非常感謝! :) – krzsz

+0

@krzsz不客氣! – AllTheTime

相關問題