0

Possible Duplicate:
AJAX Form Submission in jQuery Mobile

I have a form and I use jQuery to check and validate the form, so that, if the form is empty, it won't submit the form and will just alert "Please fill in all the fields".

function doShowError( eForm, sField, iInd, sError ) {
    var $Field = $( "[name='" + sField + "']", eForm ); // single (system) field
    if( !$Field.length ) // couple field
        $Field = $( "[name='" + sField + '[' + iInd + ']' + "']", eForm );
    if( !$Field.length ) // couple multi-select
        $Field = $( "[name='" + sField + '[' + iInd + '][]' + "']", eForm );
    if( !$Field.length ) // couple range (two fields)
        $Field = $( "[name='" + sField + '[' + iInd + '][0]' + "'],[name='" + sField + '[' + iInd + '][1]' + "']", eForm );

    //alert( sField + ' ' + $Field.length );

    $Field.parents('div:first').addClass( 'error' );

    $Field
    .parents('div:first')
        .addClass( 'error' )
        .children( 'img.warn' )
            .attr('float_info', sError)
            //.show()
            ;
}

Right now, the problem is that, whenever I click on the submit button, I get redirected to the page (which is supposed to be the user-panel ) with just a text saying:

undefined

How can I disable what the jQuery button do by default?

Edit: My event fires and the message shows , but I'm still redirected to the URL defined in the form's attribute action.

4

1 に答える 1

0

フォームのフィールド(イベントで呼び出される必要があります)を検証する関数では、検証が成功した場合などsubmitに戻る必要があります。truefalse

このようにしてaction、検証が失敗したときにフォームが属性で定義されたURLに移動するのを防ぐことができます。

あなたはこのようなことを試みるかもしれません:

$(function() {
    $("#your_form").submit(function() {
        // Define `sField`
        var sField = // SOMETHING

        // Define `iInd`
        var iInd = // SOMETHING

        // Define `sError`
        var sError = // SOMETHING

        return doShowError($(this), sField, iInd, sError);
    });
});

ここ#your_formで、はフォームのIDであり、doShowError()はフォームの入力を検証する関数であり、trueすべてのフィールドに問題がない場合は戻り、elseその場合は逆になります。

この場合、関数doShowErrorが戻るように、trueまたはfalse上記のように関数を変更する必要がある場合があります。次のように変更できます。

function doShowError( eForm, sField, iInd, sError ) {

    // `haserror`: boolean output which will equal `true` 
    // if all the fields are correct, and `else` otherwise
    var haserror = true;

    var $Field = $( "[name='" + sField + "']", eForm ); // single (system) field
    if( !$Field.length ) { // couple field
        $Field = $( "[name='" + sField + '[' + iInd + ']' + "']", eForm );
        haserror = false;
    }
    if( !$Field.length ) { // couple multi-select
        $Field = $( "[name='" + sField + '[' + iInd + '][]' + "']", eForm );
        haserror = false;
    }
    if( !$Field.length ) { // couple range (two fields)
        $Field = $( "[name='" + sField + '[' + iInd + '][0]' + "'],[name='" + sField + '[' + iInd + '][1]' + "']", eForm );
        haserror = false;
    }

    //alert( sField + ' ' + $Field.length );

    $Field.parents('div:first').addClass( 'error' );

    $Field.parents('div:first')
        .addClass( 'error' )
        .children( 'img.warn' )
            .attr('float_info', sError)
            //.show()
            ;

    return haserror;
}
于 2012-10-14T22:24:38.880 に答える