Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Conflitto tra due JavaScript

    Buona sera,
    purtroppo ho due javascript che hanno deciso di non andare d'accordo e complicarmi le giornate senza riuscire a venirne a capo..

    uno è questo:

    <select id="id-select" onchange="if (this.value) window.location.href=this.value">
    <option value="uno.php">UNO</option>
    <option value="due.php">DUE</option>
    <option value="tre.php">TRE</option>
    </select>

    che mi serve per usare gli option come link quando vengolo selezionati, mentre l'altro è questo:

    codice HTML:
    <script type="text/javascript">
        $( function() {
            $( '#id-select' ).dropdown( {
                gutter : 5,
                delay : 100,
                random : true
            });
        });
    </script>
    anch'esso importante e non posso eliminarlo perche serve per la parte estetica.
    se li uso singolarmente funzionano ma messi insieme gli option non sono piu linkabili come prima..

    come potrei risolvere questo problema?

  2. #2
    Andando per tentativi ho visto che a dar fastidio è l'istruzione .dropdown legata a sua volta ad un javascript ben piu ampio..
    io con la speranza che possa esser utile e che qualcuno abbia voglia ed il tempo di darci un occhio ve lo posto.

    codice HTML:
    ;( function( $, window, undefined ) {
    
        'use strict';
    
        $.DropDown = function( options, element ) {
            this.$el = $( element );
            this._init( options );
        };
    
        // the options
        $.DropDown.defaults = {
            speed : 300,
            easing : 'ease',
            gutter : 0,
            // initial stack effect
            stack : true,
            // delay between each option animation
            delay : 0,
            // random angle and positions for the options
            random : false,
            // rotated [right||left||false] : the options will be rotated to thr right side or left side.
            // make sure to tune the transform origin in the stylesheet
            rotated : false,
            // effect to slide in the options. value is the margin to start with
            slidingIn : false,
            onOptionSelect : function(opt) { return false; }
        };
    
        $.DropDown.prototype = {
    
            _init : function( options ) {
    
                // options
                this.options = $.extend( true, {}, $.DropDown.defaults, options );
                this._layout();
                this._initEvents();
    
            },
            _layout : function() {
    
                var self = this;
                this.minZIndex = 1000;
                var value = this._transformSelect();
                this.opts = this.listopts.children( 'li' );
                this.optsCount = this.opts.length;
                this.size = { width : this.dd.width(), height : this.dd.height() };
    
                var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
    
                    inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();
    
                this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );
    
                 this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
                 this._positionOpts();
                 if( Modernizr.csstransitions ) {
                    setTimeout( function() { self.opts.css( 'transition', 'all ' + self.options.speed + 'ms ' + self.options.easing ); }, 25 );
                }
    
            },
            _transformSelect : function() {
    
                var optshtml = '', selectlabel = '', value = -1;
                this.$el.children( 'option' ).each( function() {
    
                    var $this = $( this ),
                        val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
                        classes = $this.attr( 'class' ),
                        selected = $this.attr( 'selected' ),
                        label = $this.text();
    
                    if( val !== -1 ) {
                        optshtml +=
                             classes !== undefined ?
                                 '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
                                '<li data-value="' + val + '"><span>' + label + '</span></li>';
                    }
    
                    if( selected ) {
                        selectlabel = label;
                        value = val;
                    }
    
                } );
    
                this.listopts = $( '<ul/>' ).append( optshtml );
                this.selectlabel = $( '<span/>' ).append( selectlabel );
                this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
                this.$el.remove();
    
                return value;
    
            },
            _positionOpts : function( anim ) {
    
                var self = this;
    
                this.listopts.css( 'height', 'auto' );
                this.opts
                    .each( function( i ) {
                        $( this ).css( {
                            zIndex : self.minZIndex + self.optsCount - 1 - i,
                            top : self.options.slidingIn ? ( i + 1 ) * ( self.size.height + self.options.gutter ) : 0,
                            left : 0,
                            marginLeft : self.options.slidingIn ? i % 2 === 0 ? self.options.slidingIn : - self.options.slidingIn : 0,
                            opacity : self.options.slidingIn ? 0 : 1,
                            transform : 'none'
                        } );
                    } );
    
                if( !this.options.slidingIn ) {
                    this.opts
                        .eq( this.optsCount - 1 )
                        .css( { top : this.options.stack ? 9 : 0, left : this.options.stack ? 4 : 0, width : this.options.stack ? this.size.width - 8 : this.size.width, transform : 'none' }
     )
                        .end()
                        .eq( this.optsCount - 2 )
                        .css( { top : this.options.stack ? 6 : 0, left : this.options.stack ? 2 : 0, width : this.options.stack ? this.size.width - 4 : this.size.width, transform : 'none' }
     )
                        .end()
                        .eq( this.optsCount - 3 )
                        .css( { top : this.options.stack ? 3 : 0, left : 0, transform : 'none' } );
                }
    
            },
            _initEvents : function() {
                            var self = this;
                            this.selectlabel.on( 'mousedown.dropdown', function( event )
    {
                    self.opened ? self.close() : self.open();
                    return false;
    
                } );
    
                this.opts.on( 'click.dropdown', function() {
                    if( self.opened ) {
                        var opt = $( this );
                        self.options.onOptionSelect( opt );
                        self.inputEl.val( opt.data( 'value' ) );
                        self.selectlabel.html( opt.html() );
                        self.close();
                    }
                } );
    
            },
            open : function() {
                var self = this;
                this.dd.toggleClass( 'cd-active' );
                this.listopts.css( 'height', ( this.optsCount + 1 ) * ( this.size.height + this.options.gutter ) );
                this.opts.each( function( i ) {
    
                    $( this ).css( {
                        opacity : 1,
                        top : self.options.rotated ? self.size.height + self.options.gutter : ( i + 1 ) * ( self.size.height + self.options.gutter ),
                        left : self.options.random ? Math.floor( Math.random() * 11 - 5 ) : 0,
                        width : self.size.width,
                        marginLeft : 0,
                        transform : self.options.random ?
                            'rotate(' + Math.floor( Math.random() * 11 - 5 ) + 'deg)'
     :                        self.options.rotated ?
                                self.options.rotated === 'right' ?
                                    'rotate(-' + ( i * 5 ) + 'deg)' :
                                    'rotate(' + ( i * 5 ) + 'deg)'
                                : 'none',
                        transitionDelay : self.options.delay && Modernizr.csstransitions ? self.options.slidingIn ? ( i * self.options.delay ) + 'ms' : ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : 0
                    } );
    
                } );
                this.opened = true;
    
            },
            close : function() {
    
                var self = this;
                this.dd.toggleClass( 'cd-active' );
                if( this.options.delay && Modernizr.csstransitions ) {
                    this.opts.each( function( i ) {
                        $( this ).css( { 'transition-delay' : self.options.slidingIn ? ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : ( i * self.options.delay ) + 'ms' } );
                    } );
                }
                this._positionOpts( true );
                this.opened = false;
    
            }
    
        }
    
        $.fn.dropdown = function( options ) {
            var instance = $.data( this, 'dropdown' );
            if ( typeof options === 'string' ) {
                var args = Array.prototype.slice.call( arguments, 1 );
                this.each(function() {
                    instance[ options ].apply( instance, args );
                });
            }
            else {
                this.each(function() {
                    instance ? instance._init() : instance = $.data( this, 'dropdown', new $.DropDown( options, this ) );
                });
            }
            return instance;
        };
    
    } )( jQuery, window );

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2010
    residenza
    Pianeta Terra
    Messaggi
    1,614
    Prova a cambiare le posizioni, uno li metti in basso, l'altro in alto e viceversa; oppure uno lo metti in mezzo, l'altro alla fine e via dicendo.
    Per una bella risata vai QUI

  4. #4
    Quote Originariamente inviata da Nobody33 Visualizza il messaggio
    Prova a cambiare le posizioni, uno li metti in basso, l'altro in alto e viceversa; oppure uno lo metti in mezzo, l'altro alla fine e via dicendo.
    Non so se era un consiglio serio o no, io comunque nel dubbio ho provato a seguire il tuo consiglio e spostare i js in ogni dove ma non cambia nulla, ancora nulla da fare.

  5. #5
    Visto che usi JQuery cancella l'OnChange dal tuo tag html e aggiungi il seguente script all'interno di $(function(){ ... });

    codice:
    $("#id-select").change(function(){
    		window.location.href = $(this).val();
    	});

  6. #6
    Quote Originariamente inviata da cronenborg Visualizza il messaggio
    Visto che usi JQuery cancella l'OnChange dal tuo tag html e aggiungi il seguente script all'interno di $(function(){ ... });

    codice:
    $("#id-select").change(function(){
            window.location.href = $(this).val();
        });
    ancora niente.. ho tolto l' OnChange ed ho modificato lo script nel seguente modo:
    codice:
    <script type="text/javascript">
        $( function() {
            $( '#id-select' ).change(function(){
            window.location.href = $(this).val();
        });
    
    
            $( '#id-select' ).dropdown( {
                gutter : 5,
                delay : 100,
                random : true
        });
        });
    </script>
    Ma non funzionano i link, se invece scrivo:
    codice:
    <script type="text/javascript">
        $( function() {
            $( '#id-select' ).change(function(){
            window.location.href = $(this).val();
        });
     });
    </script>

    funzionano i link ma perdo l'aspetto grafico del Select

    come faccio per far funzionare entrambi?

  7. #7
    Scusa,
    nella fretta ho fatto un errore da principiante!

    non è
    window.location.href = $(this).val();

    ma è:
    window.location.href = $("#id-select option:selected").val();

    Poichè il valore sta nell'option, non nel tag select!

    Inoltre, per sicurezza, metti il mio codice dopo la tua istruzione dropdown({ ... });, giusto per essere sicuri che .dropdown non riscriva .change
    Ultima modifica di cronenborg; 09-11-2013 a 14:04

  8. #8
    Quote Originariamente inviata da cronenborg Visualizza il messaggio
    Scusa,
    nella fretta ho fatto un errore da principiante!

    non è
    window.location.href = $(this).val();

    ma è:
    window.location.href = $("#id-select option:selected").val();

    Poichè il valore sta nell'option, non nel tag select!

    Inoltre, per sicurezza, metti il mio codice dopo la tua istruzione dropdown({ ... });, giusto per essere sicuri che .dropdown non riscriva .change
    purtroppo credo ci sia ancora qualcosa che non va, ho fatto le modifiche che mi hai consigliato ma ancora non funziona.

    codice HTML:
    <script type="text/javascript">
        $( function() {
            $( '#id-select' ).dropdown( {
                gutter : 5,
                delay : 100,
                random : true
        });
    
            $( '#id-select' ).change(function(){
            window.location.href = $("#id-select option:selected").val();
        });
        });
    </script>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.