Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473

    document.ready multiplo

    Ciao a tutti,
    ho usato il form ajax dove invia i dati ad un altra pagina rimanendo sulla pagina del form.
    Ho preso spunto da questo link .
    Il form funziona correttamente.

    Ho uato lo stesso metodo su un altra pagina che contiene n form identici con gli id del submit numerati nel seguente modo
    1) submit del 1 form ha come id="submit0"
    2) submit del 2 form ha come id="submit1"

    nella mia funzione java ho aggiunto lo stesso codice sia per il submit1 che pre il submit 2 nel seguente modo:

    codice:
    $(document).ready(function() {
    
    		//if submit button is clicked
    		$('#submit0').click(function () {	
    		
    		alert("ok");
    			
    			//Get the data from all the fields;
    			var utente = $('input[name=utente0]');
    			
    			alert(utente.val());
    	
    			//Simple validation to make sure user entered something
    			//If error found, add hightlight class to the text field
    				
    					
    			if (utente.val()=='') {
    				utente.addClass('hightlight');
    				return false;
    			} else utente.removeClass('hightlight');
    			
    			
    			
    			//organize the data properly
    			var data = utente.val();
    			var pippo='utente=' + encodeURIComponent(data.replace(/\n\r/g,"~").replace(/\n/g,"~").replace(/\r/g,"~"));
    			//alert(pippo);
    			//var testo=comment.val();
    			
    			//var testo2=testo.replace(/String.fromCharCode(10)gi/, String.fromCharCode(93));
    			//alert(testo2);
    			//alert(testo.charCodeAt(3));
    			//testo.replace(vbCrLf,"-").replace(vbCrLf,"-").replace(/\r/gi,"-").replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/gi,"-");
    			//alert(testo.charCodeAt(4));
    					
    			//organize the data properly
    			//var data = 'comment='  + testo2;
    			//var data = 'comment='  + encodeURIComponent(comment.val());
    			//data.replace(/%0D%0A/gi,"
    ");
    			//data.replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/g, '$1$2');
    			
    			
    			
    				//alert(data);	
    				
    			//disabled all the text fields
    			$('.text').attr('disabled','true');
    			
    			//show the loading sign
    			$('.loading').show();
    			
    			//start the ajax
    			$.ajax({
    				//this is the php file that processes the data and send mail
    				url: "aggiorna_utenti.aspx",	
    				
    				//GET method is used
    				type: "POST",
    	
    				//pass the data			
    				data: pippo,		
    				
    				//Do not cache the page
    				cache: false,
    				
    				//success
    				success: function (html) {				
    					//if process.php returned 1/true (send mail success)
    					
    					if (html==1) {					
    						//hide the form
    						$('.loading').fadeOut('slow');					
    						
    						//show the success message
    						$('.done').fadeIn('slow');
    						
    						$('#mex').fadeIn('slow'); 
    						
    						$('.done').fadeOut('slow');
    						$('#mex').fadeOut('slow');
    						
    						//document.getElementById('mex2').innerHTML = comment.val();
    						
    					//if process.php returned 0/false (send mail failed)
    					} 
    					else
    					{ alert('Si è verificato un errore. Ti invitiamo a riprovare più tardi!');					
    						alert(html);
    					}
    				}		
    			});
    			
    			//cancel the submit button default behaviours
    			return false;
    		});	
    		
    		//if submit button is clicked
    		$('#submit1').click(function () {	
    		
    		alert("ok");
    			
    			//Get the data from all the fields;
    			var utente = $('input[name=utente1]');
    			
    			alert(utente.val());
    	
    			//Simple validation to make sure user entered something
    			//If error found, add hightlight class to the text field
    				
    					
    			if (utente.val()=='') {
    				utente.addClass('hightlight');
    				return false;
    			} else utente.removeClass('hightlight');
    			
    			
    			
    			//organize the data properly
    			var data = utente.val();
    			var pippo='utente=' + encodeURIComponent(data.replace(/\n\r/g,"~").replace(/\n/g,"~").replace(/\r/g,"~"));
    			//alert(pippo);
    			//var testo=comment.val();
    			
    			//var testo2=testo.replace(/String.fromCharCode(10)gi/, String.fromCharCode(93));
    			//alert(testo2);
    			//alert(testo.charCodeAt(3));
    			//testo.replace(vbCrLf,"-").replace(vbCrLf,"-").replace(/\r/gi,"-").replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/gi,"-");
    			//alert(testo.charCodeAt(4));
    					
    			//organize the data properly
    			//var data = 'comment='  + testo2;
    			//var data = 'comment='  + encodeURIComponent(comment.val());
    			//data.replace(/%0D%0A/gi,"
    ");
    			//data.replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/g, '$1$2');
    			
    			
    			
    				//alert(data);	
    				
    			//disabled all the text fields
    			$('.text').attr('disabled','true');
    			
    			//show the loading sign
    			$('.loading').show();
    			
    			//start the ajax
    			$.ajax({
    				//this is the php file that processes the data and send mail
    				url: "aggiorna_utenti.aspx",	
    				
    				//GET method is used
    				type: "POST",
    	
    				//pass the data			
    				data: pippo,		
    				
    				//Do not cache the page
    				cache: false,
    				
    				//success
    				success: function (html) {				
    					//if process.php returned 1/true (send mail success)
    					
    					if (html==1) {					
    						//hide the form
    						$('.loading').fadeOut('slow');					
    						
    						//show the success message
    						$('.done').fadeIn('slow');
    						
    						$('#mex').fadeIn('slow'); 
    						
    						$('.done').fadeOut('slow');
    						$('#mex').fadeOut('slow');
    						
    						//document.getElementById('mex2').innerHTML = comment.val();
    						
    					//if process.php returned 0/false (send mail failed)
    					} 
    					else
    					{ alert('Si è verificato un errore. Ti invitiamo a riprovare più tardi!');					
    						alert(html);
    					}
    				}		
    			});
    			
    			//cancel the submit button default behaviours
    			return false;
    		});	
    
    });
    Come potete vedere ho ripetuto la funzione per i 2 submit che hanno nome diverso

    codice:
    $('#submit0').click(function () {
    Mi piacerebbe usare solo una funzione magari dentro un ciclo, così se la mia pagina caricherà 20 form, non sono costretto a scrivere 20 funzioni identiche!!!


    Grazie!

    Ciao




    [/CODE]

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473
    up

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    sono elementi di tipo submit o button?

    cmq puoi usare questi selettori: $(":submit") o $(":button")


  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473
    si sono tutti submit,
    e hanno nome diverso.
    non cè una procedura che legge il nome o id del submit??

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    usa il selettore che ti ho indicato, dentro la funzione puoi recuperare i vari attributi(id, nome, ecc...) dell' elemento cliccato.

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473
    Ciao,
    grazie per l'aiuto.

    il mio problema è che non so proprio come recuperare l'id o il nome del pulsante cliccato.

    allora se ho capito dovrei fare cosi

    codice:
    $(document).ready(function() {
    
    		//if submit button is clicked
    		$('#submit0').click(function () {
    
    codice che recupera id tasto
    Giusto??

    Lo so che possono sembrare cpose pvvie , ma purtroppo non conosco bene javascript

    Grazie

    Ciao

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    Originariamente inviato da androita
    Ciao,
    grazie per l'aiuto.

    il mio problema è che non so proprio come recuperare l'id o il nome del pulsante cliccato.

    allora se ho capito dovrei fare cosi

    codice:
    $(document).ready(function() {
    
    		//if submit button is clicked
    		$('#submit0').click(function () {
    
    codice che recupera id tasto
    Giusto??
    No, devi usare il selettore che ti ho indicato prima:

    codice:
    $(function() {
      $(':submit').click(function () {
        //esempio per recuperare id
        $(this).attr("id");
      });
    });
    La funzione verrà richiamata per tutti i pulsanti submit che hai nella pagina.

  8. #8
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473
    Ok grazie tante!
    Stasera provo e poi ti faccio sapere!

    Ciao

  9. #9
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    473
    Ho provato e funziona perfettamente!!!
    Grazie tante!!


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.