次のコードからのユーザー入力の投稿に問題があります。情報は、送信ボタンが 2 回クリックされた後にのみ投稿されます。私のコードを見て、何か変更が必要かどうか教えてください。
<script>
function validate(form) {
fail = validateName(form.name.value)
fail += validateEmail(form.email.value)
fail += validateCity(form.city.value)
if (fail == "") {
$(function () {
$('#submitbtn').click(function (event) {
event.preventDefault();
var form = $(this).closest('form'),
action = form.attr('action');
$.post(action, form.serialize(), function (data) {
$('#errors').html("You have been successfully subscribed to our newsletter.");
})
});
});
}
else {
document.getElementById('errors').innerHTML = fail;
}
return false
}
function validateName(field) {
if (field == "") return "No name was entered.<br/>"
else if (field.length < 3) return "Please enter a valid name.<br/>"
else if (!/[a-zA-Z ]/.test(field)) return "Name can only have alphabetical characters.<br/>"
return ""
}
function validateEmail(field) {
if (field == "") return "No email was entered.<br/>"
else if (!((field.indexOf(".") > 0) && (field.indexOf("@") > 0)) || /[^a-zA-Z0-9.@_-]/.test(field)) return "The email address is invalid.<br/>"
return ""
}
function validateCity(field) {
if (field == "") return "No city was entered.<br/>"
else if (field.length < 3) return "Please enter a valid city.<br/>"
else if (!/[a-zA-Z ]*$/.test(field)) return "City can only have alphabetical characters.<br/>"
return ""
}
</script>
</head>
<body id="body-results-page">
<div class="modalDialog" id="openModal">
<div>
<a class="close" href="#close" title="Close">X</a>
<h2>Newsletter Sign-Up</h2>
<form action="" name="subscribe1" id="subscribe1" method="POST" autocomplete="off" onSubmit="return validate(this)">
<div id="errors"></div>
<input name="name" required pattern="[a-zA-Z ]*$" title="Please enter only alphabetic characters" type="text" id="name" placeholder="Your name"/>
<input name="email" required id="email" type="email" title="Please enter your email address" placeholder="Your email address"/>
<input name="city" required pattern="[a-zA-Z ]*$" title="Please enter only alphabetic characters" id="city" placeholder="Your city"/>
<div id="buttons">
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset"/>
<button type="submit" name="submit" id="submitbtn" class="submitbtn" value="Submit this!">Subscribe Now!</button>
<br style="clear:both;">
</div>
<input type="hidden" name="MM_insert" value="subscribe1">
</form>