简体   繁体   English

如何使此代码中不需要的字段?

[英]How can I make a field NOT required in this code?

So I took this code from this website . 所以我从这个网站上获取了这段代码。 I don't want to have all the fields being required. 我不想要求所有字段。 Like, let's say, full middle name . 比方说, 全名 I want that field to be optional (hence, not affecting the function for checking which fields have error). 我希望该字段是可选的(因此,不影响检查哪些字段有错误的功能)。 How can I do it? 我该怎么做?

    $(function() {
    /*
    number of fieldsets
    */
    var fieldsetCount = $('#formElem').children().length;

    /*
    current position of fieldset / navigation link
    */
    var current     = 1;

    /*
    sum and save the widths of each one of the fieldsets
    set the final sum as the total width of the steps element
    */
    var stepsWidth  = 0;
    var widths      = new Array();
    $('#steps .step').each(function(i){
        var $step       = $(this);
        widths[i]       = stepsWidth;
        stepsWidth      += $step.width();
    });
    $('#steps').width(stepsWidth);

    /*
    to avoid problems in IE, focus the first input of the form
    */
    $('#formElem').children(':first').find(':input:first').focus(); 

    /*
    show the navigation bar
    */
    $('#navigation').show();

    /*
    when clicking on a navigation link 
    the form slides to the corresponding fieldset
    */
    $('#navigation a').bind('click',function(e){
        var $this   = $(this);
        var prev    = current;
        $this.closest('ul').find('li').removeClass('selected');
        $this.parent().addClass('selected');
        /*
        we store the position of the link
        in the current variable 
        */
        current = $this.parent().index() + 1;
        /*
        animate / slide to the next or to the corresponding
        fieldset. The order of the links in the navigation
        is the order of the fieldsets.
        Also, after sliding, we trigger the focus on the first 
        input element of the new fieldset
        If we clicked on the last link (confirmation), then we validate
        all the fieldsets, otherwise we validate the previous one
        before the form slided
        */
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
            if(current == fieldsetCount)
                validateSteps();
            else
                validateStep(prev);
            $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
        });
        e.preventDefault();
    });

    /*
    clicking on the tab (on the last input of each fieldset), makes the form
    slide to the next step
    */
    $('#formElem > fieldset').each(function(){
        var $fieldset = $(this);
        $fieldset.children(':last').find(':input').keydown(function(e){
            if (e.which == 9){
                $('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
                /* force the blur for validation */
                $(this).blur();
                e.preventDefault();
            }
        });
    });

    /*
    validates errors on all the fieldsets
    records if the Form has errors in $('#formElem').data()
    */
    function validateSteps(){
        var FormErrors = false;
        for(var i = 1; i < fieldsetCount; ++i){
            var error = validateStep(i);
            if(error == -1)
                FormErrors = true;
        }
        $('#formElem').data('errors',FormErrors);   
    }

    /*
    validates one fieldset
    and returns -1 if errors found, or 1 if not
    */
    function validateSteps(){
        var FormErrors = false;
        for(var i = 1; i < fieldsetCount; ++i){
            var error = validateStep(i);
            if(error == -1)
                FormErrors = true;
        }
        $('#formElem').data('errors',FormErrors);
    }

    /*
    validates one fieldset
    and returns -1 if errors found, or 1 if not
    */

    function validateStep(step){
        if(step == fieldsetCount) return;

        var error = 1;
        var hasError = false;
        $('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
            var $this       = $(this);
            var valueLength = jQuery.trim($this.val()).length;

            if(valueLength == '')
            {   
                hasError = true;

                $this.css('background-color','red');        
            }
            else
            {
                $this.css('background-color','yellow'); 
            }

        }


        );


        var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
        $link.parent().find('.error,.checked').remove();

        var valclass = 'checked';

        if(hasError)
        {

            error = -1;
            valclass = 'error';
        }
        $('<span class="'+valclass+'"></span>').insertAfter($link);

        return error;
    }


    /*
    if there are errors don't allow the user to submit
    */
    $('#registerButton').bind('click',function(){
        if($('#formElem').data('errors')){
            alert('Please correct the errors in the Form');
            return false;
        }   
    });
});

inside validateStep function on line where you perform the find just add this tag after :input like below: 在执行查找的行中的validateStep函数中,只需在:input之后添加此标记,如下所示:

$('#formElem').children(':nth-child('+ parseInt(step)+')').find(':input[required]:not(button)')

You just have to use the required html attribute inside your web page and when you will run the validateStep function, it will ignore all fields without the required attribute. 您只required在网页内使用required html属性,当您运行validateStep函数时,它将忽略没有required属性的所有字段。

look at mdn doc for more informations. 查看mdn doc了解更多信息。 https://developer.mozilla.org/en-US/docs/Web/CSS/:required https://developer.mozilla.org/zh-CN/docs/Web/CSS/:必填

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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