0

スクリプトを機能させることができません。実行すると常にエラーが返されます

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: false
    });
    if (success == "true")
    {
        function()
        {
            window.alert("Report Sent!");
            //write a confirmation to the user
            document.getElementById("update").innerHTML="Report Sent!";
            setTimeout(function()
            {
                document.getElementById("update").innerHTML="";
            },3000);
        }
    }
});

これに対する良い解決策は何ですか?

4

2 に答える 2

2

ここに問題があります:

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: false
    });

validationEngineオブジェクトリテラルをメソッドに渡します。successプロパティは関数である必要があります。この関数には、検証が成功したときに実行されるように表示するコードが含まれています。代わりに、falseに設定してから、次のように続行します。

if (success == "true")
{
     function()
     {

実際にオブジェクトプロパティである変数を使用しているだけでなく(したがって、EVILであるグローバル変数を作成している)、関数を宣言していますが、それを呼び出していません。successの値をチェックしていて、文字列であると期待していることに気づきました。phpスクリプトの戻り値は、引数として成功コールバック関数に渡されます。私もそれを反映するように関数を自由に変更しました:

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: function(response)//response is what the php script returns
        {
            //the function from your if branch goes here
        }
    });

そして、あなたは大丈夫でなければなりません。完全なコードは次のようになります。

$(document).ready(function()
{//notice the curly braces around what you're passing to validationEngine
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: function(response)
        {
            if (response !== 'true')
            {
                if (window.console)
                {
                    console.log(response);
                    alert('check your console to see the response');
                }
                else
                {
                    alert(response);
                }
                return;
            }
            alert("Report Sent!");//no need for window here
            document.getElementById("update").innerHTML="Report Sent!";
            setTimeout(function()
            {
                document.getElementById("update").innerHTML="";
            },3000);
        }
    });
    //As I said, you're passing an object, so you could write the code above like so:
    var validationArgs = {ajaxSubmit:true,ajaxSubmitFile:'note/note.php'};
    validationArgs.success = function()
    {
        //your function
    };
    $('#form1').validationEngine(validationArgs);
});
于 2012-09-26T19:50:52.773 に答える
0

これにはコールバック関数を使用する必要があります。

$(document).ready(function() {
    var update = $("#update");
    // Callback 
    var onSuccess = function() {
        alert("Report Sent!");

        //write a confirmation to the user           
        update.html("Report Sent!");

        setTimeout(function(){
             update.html("");
        },3000);
    };

    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: onSuccess
   });
});
于 2012-09-26T19:57:20.820 に答える