简体   繁体   中英

dropdown focus on page and enter key press check using jquery/javascript

a dropdown with focus and enter key pressed validate for form.

I have a form with elements that are validated by jquery (in the validator function as below) If I have a textbox, and I enable focus on it and press enter, I get all of Javascript validations.

$( document ).ready(function() {
        var container = $('div.errors');

            $("mydropdownId").focus(); //tried this , focus works, but enter key press check  doesn't work 
            // validate the form when it is submitted
        var validator = $("#coverageForm").validate({
        invalidHandler: function() {
            $('#form_error_notice').css('display', 'block');
            $('.server_error_notice').css('display', 'none');
                $('.errorsServer').css('display', 'none');
                hintAgain();
        },
         submitHandler: function(){

            DoDisable(); //changes couple of divs and does document.form.submit();
            },
      errorContainer: $("#coverageForm .errors"),
                errorLabelContainer: $("ul", "#coverageForm .errors"),
                    wrapper: 'li',
                    meta: "validate"
        });
    // Control input in numeric form fields
    $('input.numeric').numeric();
    // Initiate overlays
    $('.boxy').boxy({modal: true});
});

I am new to jquery, anyone who could guide me towards the light? I don't want to change the validation that jquery provides. It is a stringe requirement from my "client"!

Thanks in advance!

The default behavior for a html form is that when you click enter the form is submitted. So that kicks JQuery.Validator off to validate the fields on the form.

So to get around this, you might have to capture the keydown or keypress events, get the enter key down, then move to the next field manually (call focus on the next control)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM