$(“#submit”)。点击不工作:/

时间:2012-07-10 12:55:39

标签: javascript jquery

我已经尝试了所有我能想到的“发送”按钮,但我不是很擅长javascript :(

http://www.kimscakes.biz/contactme.php

这里有人可以告诉我哪里出错了?

非常感谢

宝石

1 个答案:

答案 0 :(得分:3)

您在元素存在之前绑定提交,在表单之后移动代码或将其放在$(document).ready内:

jQuery(document).ready(function () {
    jQuery("#submit").click(function () {
        console.log("button clicked");
        // declare these vars
        var name = jQuery("#name");
        var email = jQuery("#email");
        var message = jQuery("#message");
        var human = jQuery("#human");
        var success = jQuery("#success");
        var error = jQuery("#error");
        // reset these vars
        success.html("");
        error.html("");
        // ajax call to script.php
        jQuery.getJSON("mailer.php", {
            name: name.val(),
            email: email.val(),
            message: message.val(),
            human: human.val()
        }, function (html) {
            // if html from script.php == 1, happy days
            if (html == '1') {
                jQuery("#contact").hide()
                success.html("Thank You, your message has been sent.")
                error.show()
            }
            else {
                error.html(html)
            }
        });
    });
});