在javascript

时间:2018-03-06 15:30:15

标签: javascript html forms

这是我的第一篇文章,所以不要判断我是否做错了,因为我还在学习,但我有一个问题,我正在制作一个表格而且我遇到了问题。当我离开按钮时没有任何额外的东西,当我测试表格时,我得到了很多次,因为我点击了按钮,所以我在JS中使用了这里的代码片段并制作了按钮,所以它消失了,问题是当我没有&# 39;正确填写表格它没有发送条目,所以问题是如何在表格中添加条件,这样按钮只有在每个字段写得正确时才会消失?我会添加整个HTML,但它现在有很多代码,因为我在这里测试的东西几乎就是表单代码。

我的另一个想法是尝试编辑PHP代码,但老实说我不知道​​这个,因为我的朋友做了。

我还搜索谷歌和这里的一些提示,但无法找到,请提前感谢您的帮助。



$("#hideme").click(function(){
   $(this).hide();
});

<form method="post" action="thankyou.php" style="overflow:hidden" class="accident-form4">
    <input type="hidden" name="lang" value="pl">
    <div class="form-group required">
        <label for="name">Imię i nazwisko<span></span></label>
        <input type="text" class="form-control inputToUpper" name="firstname" placeholder="Andrzej Kowalski" required="required" pattern=".*\S+.*" title="Wpisz swoje imię">
    </div>
    <div class="form-group required">
        <label for="email">Email<span></span></label>
        <input type="email" class="form-control" required="required" name="email" placeholder="np. andrzej.kowalski@gmail.com" title="Wpisz poprawny adres email!" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
    </div>
    <div class="form-group">
        <label for="tel">Numer telefonu<span></span></label>
        <input type="text" name="tel" placeholder="079 1234 5678" class="form-control" required="required" size="12" value="">
    </div>
    <label for="hour">Preferowana godzina kontaktu</label>
    <select class="form-control" name="hour" placeholder="" title="" />
    <option>Dowolna</option>
    <option>Rano</option>
    <option>Po południu</option>
    <option>Wieczorem</option>
    </select>

    <input type="submit" name="lead" class="send" id="hideme" value="ROZPOCZNIJ CLAIM →">
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以在隐藏按钮之前检查所需字段的值。

$("#hideme").click(function(){

    if (document.getElementsByName('firstname')[0].value != '' &&
        document.getElementsByName('email')[0].value != '' &&
        document.getElementsByName('tel')[0].value != '') {

        $(this).hide();

    }

});

或再次在Javascript中使用您的RegExp。

$("#hideme").click(function(){

    if (document.getElementsByName('firstname')[0].value.match (/.*\S+.*/) &&
        document.getElementsByName('email')[0].value.match (/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/) &&
        document.getElementsByName('tel')[0].value.match (/[0-9]+/)) {

        $(this).hide();

    }

});

这应该是技巧(但我没有测试它)。