我需要检查在jQuery的响应中是否找到了ID
但它一直给我警报alert("else");
!
gastenboek.js
$(document).ready(function() {
$("#checkbox_warnings").on("change", function() {
if (!this.checked) {
$(this).attr("checked", false);
} else {
$("#warning_checkbox").slideUp(function () {
$(this).remove();
});
}
});
$("#gastenboek").submit(function(event){
event.preventDefault();
var form = $(this);
var url = "gastenboek_handler.php?action=check_gastenboek_form";
var posting = $.post(url, form.serialize());
posting.done(function(data) {
if ($("#er-is-error").length) {
var errors = jQuery.parseJSON(data);
$("#gb-errors").slideDown().empty().append(errors);
alert("if");
} else {
alert("else");
$("#gb-errors").empty().slideUp();
}
});
});
});
确实存在错误,但它没有slideDown
ID #gb-errors
我很确定这一点,因为在控制台中它会给出错误的响应!
gastenboek_handler.php
<?php
include_once "/core/init.php";
if ($_GET['action'] == 'check_gastenboek_form')
{
if (!empty($_POST['email']) && !preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $_POST['email']))
{
$errors[] = "Dit is geen geldig e-mail adres!";
}
if (empty($_POST['msg']))
{
$errors[] = "Het berichten veld mag niet leeg zijn.";
}
else if (strlen($_POST['msg']) < 15)
{
$errors[] = "Het bericht mag niet minder dan 15 karakters hebben!";
}
else if (strlen($_POST['msg']) > 2500)
{
$errors[] = "Het bericht mag niet langer dan 2500 karakters hebben!";
}
if (!empty($errors))
{
foreach($errors as $error)
{
$return[] = '<span class="glyphicon glyphicon-warning-sign" style="padding-right:5px; color:red;"></span><a class="errors" style="color:red;">' . $error . '</a><br>';
}
echo json_encode($return);
echo '<div id="er-is-error"></div>';
}
else
{
gb_post($_POST);
}
}
?>
希望你们知道一些事情!