Dou*_*ith 2 javascript php ajax firefox jquery
它可以在这里找到:http://syllableapp.com/test
基本上,在Safari,Chrome,Opera,Webkit Nightly等中,表单的工作方式非常精确.在Firefox中,提交它只是...没有做任何事情.为什么是这样?
这是我的JavaScript:
$(document).ready(function() {
$('input[type="submit"]').click(function() {
event.preventDefault();
var email = $.trim($('.email').val());
var emailRegEx = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (email == "" || !emailRegEx.test(email)) {
$(this).effect("shake", { times:2 }, 75);
}
else {
var data = "email=" + email;
$.ajax({
type: "POST",
url: "register_email.php",
data: data,
success: function(data) {
if (data == 1) {
$('form').hide();
$('form').html("<p class='success'>You'll be notified! Welcome aboard.</p>");
$('form').fadeIn(300);
}
else {
$('form').hide();
$('form').html("<p class='error'>Dang, there was an error. <a href='mailto:me@christianselig.com'>Email me?</a></p>");
$('form').fadeIn(300);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: " + errorThrown);
$('form').hide();
$('form').html("<p class='error'>Dang, there was an error. <a href='mailto:me@christianselig.com'>Email me?</a></p>");
$('form').fadeIn(300);
}
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
我的PHP:
<?php
$db = new mysqli("localhost", "swuclu_emailer", "etreadmill:(", "swuclu_syllable_emails");
if ($db->connect_error) {
echo "0";
}
else {
$email = $_POST["email"];
$stmt = $db->prepare("INSERT INTO emails (email) VALUES (?)");
$stmt->bind_param("s", $email);
$stmt->execute();
echo 1;
}
?>
Run Code Online (Sandbox Code Playgroud)
除Firefox之外的每个浏览器究竟会发生什么?
在页面更改时,控制台中的错误将保持不变.
你会看到这个错误:
ReferenceError: event is not defined
http://syllableapp.com/test/scripts/scripts.js
Line 3
Run Code Online (Sandbox Code Playgroud)
它带你到代码:
$(document).ready(function() {
$('input[type="submit"]').click(function() {
event.preventDefault(); //<--- here
Run Code Online (Sandbox Code Playgroud)
看着那个,你看到事件没有定义.因此错误.
因此,将变量事件添加到函数参数列表中
$('input[type="submit"]').click(function(event) { //<-- added variable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |