この例を使用しています:http://youhack.me/2010/05/04/username-availability-check-in-registration-form-using-jqueryphp/フォームを作成します(データベースに追加する前に確認ページに移動します - 後の簡単な部分)。フォームを送信する前にユーザー名が利用可能かどうかを確認してください。私の人生はイムはそれで間違っているのかを把握するために私がすることはできません:Pフォーム提出前にユーザー名を確認する - JQUERY/AJAX
はMY jqueryの/ AJAXは以下の通りです:(それが呼び出すPHPファイルは、私はここにそれを置く必要がない正常に動作します)
$(document).ready(function()
{
$(".form").submit(function()
{
var username = $(".userID").val();
$.ajax({
type: "POST",
url: "id_check.php",
data: "userID="+ username,
success: function(response){
if(response=="NO"){
alert("no");
}
else if(response=="YES"){
alert("yes");
}
}
});
return false; //if i take this line out it submits either way
});
});
ユーザー名の入力時に正しいアラートがポップアップするので、PHPが動作することがわかりますが、レスポンスもNOのときにレスポンスがYESの場合はフォームを送信できません。私は$(form).submit()やIF文中のさまざまな返品などを含む多くの異なることを試しましたが、役に立たないものです。
私は基本的に名前が取られていないときに提出する必要があり、それが提出されていないときは...どんな助けも大歓迎!!
$(".form").submit(function(e)
{
var username = $(".userID").val();
//cache the current form
var form = this;
//check if the event is started by the user or by your success function
//if it's triggered by the user, check for the username
if (e.originalEvent !== undefined){
$.ajax({
type: "POST",
url: "id_check.php",
data: "userID="+ username,
success: function(response){
if(response=="NO"){
alert("no");
}
else if(response=="YES"){
//submit the form you have cached
$(form).submit();
alert("yes");
}
}
});
//This is to prevent the form from submitting when you click the submit button
return false; //if i take this line out it submits either way
}
});
[.submit()が正しく動作しない可能性があります。unbind()は助けますが、副次的な影響を受けます:P](http://stackoverflow.com/questions/8688532/submit-not-working-correctly-unbind -helps-but-comes-a-side-affect-p) –