Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it L'avatar di lnessuno
    Registrato dal
    Feb 2002
    Messaggi
    2,732

    [jQuery] $(this) va fino a un certo punto, poi smette...

    Sto tentando disperatamente di far funzionare questo codice... sembra andare tutto bene, la richiesta ha successo ed entro nell'if (result == "hidden")... se faccio un alert di testo o newurl funzionano, però non mi cambiano il testo del link ne il suo href... cosa sbaglio?

    codice:
            $("#tabswitch a").click(function (e) {
    
                e.preventDefault();
    
                var url = $(this).attr("href");
    
                var testo = $(this).text();
    
    			
    
                $.ajax({
    
                    url: $(this).attr("href"),
    
                        async: false,
    
                        success: function(result) {
    
    
    
                        if (result == "hidden") {
    
                            var testo = testo.replace("nascosta","visualizzata");
    
                            var newurl = url.replace("display","hide");
    
                            $(this).text(testo);
    
                            $(this).attr("href") = newurl;
    
                        } else {
    
                            var testo = testo.replace("visualizzata","nascosta");
    
                            var newurl = url.replace("hide","display");
    
                            $(this).text(testo);
    
                            $(this).attr("href") = newurl;
    
                        }
    
                    }
    
                });
    
                return false;
    
            });

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2009
    Messaggi
    636
    sostrituisci questo

    Codice PHP:
     $(this).text(testo);

     $(
    this).attr("href") = newurl
    con questo

    Codice PHP:

    $(this).attr("innerHTML"testo);
    $(
    this).attr("href"newurl); 

  3. #3
    Utente di HTML.it L'avatar di lnessuno
    Registrato dal
    Feb 2002
    Messaggi
    2,732
    Niente da fare... non me ne capacito proprio

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2009
    Messaggi
    636
    forse nella funzione success $(this) non referenzia $("#tabswitch a"), prova a sostituire a $(this) il selettore $("#tabswitch a")

  5. #5
    Utente di HTML.it L'avatar di lnessuno
    Registrato dal
    Feb 2002
    Messaggi
    2,732
    L'avevo pensato ma non avevo fatto la prova in effetti... in questo modo mi corregge correttamente tutti i link di #tabswitch.

    Non è ancora quello che mi serve, ma almeno grazie a te ho scoperto che $(this) non funziona dentro success... grazie

  6. #6
    Utente di HTML.it L'avatar di lnessuno
    Registrato dal
    Feb 2002
    Messaggi
    2,732
    Ci sono diventato scemo, ma alla fine ho vinto io; come mi hai fatto notare this non funziona nella funzione success, quindi ho dovuto assegnare un id ad ogni link e puntare su quello...
    codice:
            $("#tabswitch a").click(function (e) {
    
                e.preventDefault();
    
    
    
                var url = $(this).attr("href");
    
    			var self = "#" + $(this).attr("id");
    
                var testo = $(this).text();
    
                
    
                $.ajax({
    
                    url: $(this).attr("href"),
    
    				async: false,
    
    				success: function(result) {
    
    					if (result == "hidden") {
    
    						var testo2 = testo.replace("nascosta","visualizzata");
    
    						var newurl2 = url.replace("hide","display");
    
    						$(self).text(testo2);
    
    						$(self).attr("href", newurl2);
    
    					} else {
    
    						var testo2 = testo.replace("visualizzata","nascosta");
    
    						var newurl2 = url.replace("display","hide");
    
    						$(self).text(testo2);
    
    						$(self).attr("href", newurl2);
    
    					}
    
    				}
    
                });
    
            });
    [/PHP]

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.