Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   jquery select (https://javascript.ru/forum/dom-window/42667-jquery-select.html)

rss 05.11.2013 11:41

jquery select
 
Форма состоит из:
второй type меняет вывод полей поля
первый select deal, меняет type и то что поменялось type ом
другие выводимые

кто-нибудь помогите сократить код:

var result2;

function iter_in_required(elements, type) {
    var result = true;
    $(elements).each(function(){ // каждому элементу присвоить: // element ?
        if (type==0) {
            if ($(this).is('[type="text"]') && !$(this).val() ) {
                result = false; $(this).nextAll('.err').eq(0).css('display','inline');
            }
            else if ($(this).is('textarea') && !$(this).val() ) {
                result = false; $(this).nextAll('.err').eq(0).css('display','inline');
            }
            else if ( $(this).is('select') && !$('option:selected',this).val() ) {
                result = false; $(this).nextAll('.err').eq(0).css('display','inline');
            }
        }
        else {
            var deals = $('select[name="deal"] option:selected').val();
            if (deals=='prodazha') {
                if ( $(this).parent().hasClass('t'+type) &&  $(this).is('[type="text"]') && !$(this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).parent().hasClass('t'+type) && $(this).is('textarea') && !$(this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).parent().hasClass('t'+type) &&  $(this).is('select') && !$('option:selected',this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).closest('div').hasClass('t'+type) && $(this).is('[type="checkbox"]') ) {
                    var name = $(this).attr('name');
                    result2 = false;
                    $('.param [name="'+name+'"]').each( function(){
                        if ( $(this).is(':checked') ) {result2 = true;}
                    } );
                    if (!result2) {
                        $(this).closest('ul').nextAll('.err').eq(0).css('display','inline');
                    }
                }
            } else {
                if ( $(this).parent().hasClass('a'+type) &&  $(this).is('[type="text"]') && !$(this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).parent().hasClass('a'+type) && $(this).is('textarea') && !$(this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).parent().hasClass('a'+type) &&  $(this).is('select') && !$('option:selected',this).val() ) {
                    result = false;
                    $(this).nextAll('.err').eq(0).css('display','inline');
                }
                else if ( $(this).closest('div').hasClass('a'+type) && $(this).is('[type="checkbox"]') ) {
                    var name = $(this).attr('name');
                    result2 = false;
                    $('.param [name="'+name+'"]').each( function(){
                        if ( $(this).is(':checked') ) {result2 = true;}
                    } );
                    if (!result2) {
                        $(this).closest('ul').nextAll('.err').eq(0).css('display','inline');
                    }
                }
            }
        }
    });
    return result;
}

$(document).ready(function(){
/* здесь сложновато, не получилось совместить 3: когда приходишь сюда, когда уже выбрано; пустые поля; изменить то что выбрано */
    var deals = $('select[name="deal"] option:selected').val();
    if (deals=='prodazha') {
        $('.t'+$('select[name="types"] option:selected').val()).show();
        var type = $('select[name="types"] option:selected').val();
        $('.hidden').hide();
        $('.t'+type, '.param').show();
        $('.err').hide();
    } else {
        $('.a'+$('select[name="types"] option:selected').val()).show();
        var type = $('select[name="types"] option:selected').val();
        $('.hidden').hide();
        $('.a'+type, '.param').show();
        $('.err').hide();
    }

    $('select[name="deal"]').change(function() {
        var deals = $('option:selected',this).val();
        if (deals == 'prodazha') { result = false;
            $('.a'+$('select[name="types"] option:selected').val()).hide();
            $('.t'+$('select[name="types"] option:selected').val()).show();
            $('select[name="types"]').change(function(){
                var type = $('option:selected',this).val();
                $('.hidden').hide();
                $('.t'+type, '.param').show();
                $('.err').hide();
            });
        } else { result = false;
            $('.t'+$('select[name="types"] option:selected').val()).hide();
            $('.a'+$('select[name="types"] option:selected').val()).show();
            $('select[name="types"]').change(function(){
                var type = $('option:selected',this).val();
                $('.hidden').hide();
                $('.a'+type, '.param').show();
                $('.err').hide();
            });
        }
    });

        $('.t'+$('select[name="types"] option:selected').val()).show();
        //$('.a'+$('select[name="types"] option:selected').val()).show();
        $('select[name="types"]').change(function(){
            var type = $('option:selected',this).val();
            $('.hidden').hide();
            $('.t'+type, '.param').show();//.trigger('change');
            $('.err').hide();
        });


    // реакция при нажатии Сохранить
    $('.span6 .bg-color-pink.fg-color-white').click(function(){
        $('.err').hide();
        var type = $('select[name="types"] option:selected').val();
        var result = iter_in_required($('.location .required'),0);
        if (result) result = iter_in_required($('.other-param .required'),0);
        else iter_in_required($('.other-param .required'),0);
        if (result) result = iter_in_required($('.cost .required'),0);
        else  iter_in_required($('.cost .required'),0);


        if (type != ''){
           if (result) result = iter_in_required($('.param .required'),type);
           else iter_in_required($('.param .required'),type);
        }
        else {
            $('select[name="types"]').nextAll('.err').eq(0).css('display','inline');
            result=false;
        }

        $('.input-control.file input').each(function(){
            if (  $(this).val() && !$(this).parent().nextAll('div.input-control').eq(0).find('input').val()  && !$(this).parent().nextAll('.input-control').eq(1).find('input').is(':checked')  && !$(this).parent().find('a').length )
            {
            result=false;
            $(this).parent().nextAll('.err').eq(1).css('display','inline');
            }
            else if (  !$(this).val() && $(this).parent().nextAll('div.input-control').eq(0).find('input').val()  && !$(this).parent().nextAll('.input-control').eq(1).find('input').is(':checked') && !$(this).parent().find('a').length )
            {
            result=false;
            $(this).parent().nextAll('.err').eq(0).css('display','inline');
            }
            else if (  $(this).parent().find('a').length && !$(this).parent().nextAll('.input-control').eq(1).find('input').is(':checked') && !$(this).parent().nextAll('div.input-control').eq(0).find('input').val()  )
            {
            result=false;
            $(this).parent().nextAll('.err').eq(0).css('display','inline');
            }
        });

        if (result) $('#real-form').submit();
        else alert('Заполните обязательные поля.');

    });


Часовой пояс GMT +3, время: 22:14.