Form的input type =“submit”似乎没有触发onsubmit处理程序

时间:2013-03-20 09:45:31

标签: javascript html forms validation onsubmit

我试图通过onsubmit触发javascript函数来验证我的表单。

我知道函数本身是有效的,因为我可以在表单标记之后立即在脚本标记内调用它,并且我收到了console.log消息,并且它创建的错误消息显示在表单上。

PHP验证本身有效,但我只能通过放置在div上的javascript .submit实际提交表单。显然,因为这绕过了任何客户端验证,我不能这样做,因此我用div id="submit"替换了我的input type="submit"

我一直在寻找其他的例子,包括过去我自己编写的表格,我知道工作,看起来完全没有答案。这可能非常容易,而且由于某种原因,我在过去的6个小时内无法也无法看到它。 O_O

这是我的表单标记:

<form name="emailus" id="emailus" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return validateForm();">
    <a href="mailto:example@email.com">
        <h3><i class="icon-envelope-alt icon-large"></i>Send us an email<span>: example@email.com</span></h3>
    </a>

    <div class="half">
        <fieldset class="name">
            <label for="cf_name"><h4>Name <span>*</span></h4></label>
            <input type="text" name="cf_name" class="textualformfield" alt="Jane Doe" pattern="^[a-zA-Z'\s]+$">
        </fieldset>
        <fieldset class="emailaddress">
            <label for="cf_email"><h4>E-mail <span>*</span></h4></label>
            <input type="text" name="cf_email" class="textualformfield" alt="janedoe@email.com" pattern="{long string of regex}">
        </fieldset>
        <fieldset class="phonenumber">
            <label for="cf_phonenumber"><h4>Phone</h4></label>
            <input type="tel" name="cf_phonenumber" class="textualformfield" alt="555-555-5555">
        </fieldset>
        <fieldset class="eventdate">
            <label for="cf_eventdate"><h4>Event Date</h4></label>
            <input type="text" name="cf_eventdate" class="textualformfield" alt="May 25th, 2012">
        </fieldset>
        <fieldset class="location">
            <label for="cf_location"><h4>Location</h4></label>
            <input type="text" name="cf_location" class="textualformfield" alt="The Church">
        </fieldset>
    </div>

    <div class="half">
        <textarea name="cf_message" class="textualformfield" alt="Your Message"></textarea>
    </div>

    <input type="submit" for="emailus" name="submit" id="submit" value="Submit">
</form>

我尝试过几次onsubmit="return validateForm();"的不同迭代 - 有或没有分号,将其写为onSubmitonsubmit ......我不知道。我根本看不出这有什么问题。任何人吗?

以下是要调用的函数onsubmitvalidateForm。它位于另一个文件中,但是在表单中始终包含该文件,并且我已确保此函数不在$(document).ready范围内,并且全局可用,因为我已经在脚本标记中直接跟随它形式。

var validateForm = function() {
    var valid = true;

    jQuery('p.validationhelpers').remove();

    if (document.emailus.cf_email.value == "" || document.emailus.cf_email.value == "janedoe@email.com") {
        jQuery('.emailaddress').append("<p class='validationhelpers'>Please enter an email address.</p>");
        jQuery('.emailaddress>input').focus();
        valid = false;
    }

    if (document.emailus.cf_name.value == "" || document.emailus.cf_name.value == "Jane Doe") {
        jQuery('.name').append("<p class='validationhelpers'>Please enter your name.</p>");
        jQuery('.name>input').focus();
        valid = false;
    }
    console.log("I was triggered");

    return valid;
}

编辑2:添加了PHP验证/邮政编码:

<?php if (!empty($_POST) ) { 
    $field_name = $_POST['cf_name'];
    $field_email = $_POST['cf_email'];
    $field_phone = $_POST['cf_phonenumber'];
    $field_eventdate = $_POST['cf_eventdate'];
    $field_location = $_POST['cf_location'];
    $field_message = $_POST['cf_message'];

    $mail_to = 'example@email.com';

    //final attempt at validation or email submission preventation
    if ($field_name == "Jane Doe" || empty($field_name) || $field_email == "janedoe@email.com" || empty($field_email)) {
        return false;
    }


    if (!($field_eventdate == "May 25th, 2012") && !empty($field_eventdate) && !($field_name == "Jane Doe") && !empty($field_name)) {
        $subject = 'Request for '.$field_date. ' from '.$field_name . 'via thiswebsite.com';
    } else {
        $subject = $field_name . ' has contacted you via thiswebsite.com';
    }

    if (!($field_name == "Jane Doe") && !empty($field_name)) {
        $body_message = 'Client\'s Name: '.$field_name."\n\n";
    }
    if (!($field_email == "janedoe@email.com") && !empty($field_email)) {
        $body_message .= 'E-mail: '.$field_email."\n\n";
    }
    if (!($field_phone == "555-555-5555") && !empty($field_phone)) {
        $body_message .= 'Phone: '.$field_phone."\n\n";
    }
    if (!($field_eventdate == "May 25th, 2012") && !empty($field_eventdate)) {
        $body_message .= 'Event Date: '.$field_eventdate."\n\n";
    }
    if (!($field_location == "The Church") && !empty($field_location)) {
        $body_message .= 'Location: '.$field_location."\n\n";
    }
    if (!($field_message == "Your Message") && !empty($field_message)) {
        $body_message .= 'Message: '. "\n".$field_message;
    }

    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);

    $body_message = stripslashes($body_message);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                jQuery('#contactus').before("<hr class='confirmationhelpers'><p class='confirmationhelpers'>Your e-mail has been sent!<br/>Thank you! We'll contact you shortly.</p><hr class='confirmationhelpers'>");
            });
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                alert('It seems there\'s been an error. Please, send an email to example@email.com to request your appointment.');
            });
        </script>
    <?php
    }
}

?>

为了清楚 - 当我点击提交按钮时,绝对没有任何反应。

2 个答案:

答案 0 :(得分:2)

您的jQuery阻止了此处提交的表单:

//this hides the contact form if the overlay is clicked. children is there to prevent the children from also having this effect.
    $('.overlay_vertical_align').click(function() {
        $('.grey_overlay').fadeOut('slow');
    }).children().click(function(e) {
        return false;
    });

我的意思是,当我调试您的网站并按下提交按钮时,我会到达上面的return false;,然后事件处理结束。

答案 1 :(得分:0)

没有任何反应的原因是您使用新的html5'pattern'属性,该属性在它到达验证功能之前尝试对您的输入进行正则表达式验证。删除pattern属性,然后重试。