Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di otello
    Registrato dal
    May 2003
    Messaggi
    296

    Inserimento in textbox di valore da DDL

    Ciao a tutti, è da un po' che non avevo bisogno d'aiuto.
    Allora: ho una textbox con del testo (alcune righe), ho una dropdownlist ed un button . Alla pressione del button dovrei inserire l'elemento selezionato della DDL in un punto specifico del text box (dove posiziono il cursore). Avete qualche idea di come posso fare?
    Merçi.

  2. #2
    intanto scusa la domanda ma come fai?

    tu cliccki sul textbox e poi clicchi sul button, a questo punto il cursore non è + nel texbox, quindi non vedo soluzione.

  3. #3
    Utente di HTML.it L'avatar di otello
    Registrato dal
    May 2003
    Messaggi
    296
    Il principio mi sembrava identico all'inserimento di uno smile in un punto preciso del text box, sempre un click devo fare, che sia sullo smile o sul botton...

  4. #4

  5. #5
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    vedi se ti può servire:
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Pagina senza titolo</title>
    
        <script language="javascript" type="text/javascript">
    // <!CDATA[
    
    function Button1_onclick() 
    {
        var ta = document.getElementById("message");
        var valore = "bold";
        insert_bbcode(ta, valore);
    }
    
    function Button2_onclick() 
    {
        var ta = document.getElementById("message");
        var valore = document.getElementById("Select1").value;
        insert_bbtext(ta, valore);
    }
    
    function insert_bbcode(v, bbcode){
    	try {
    		if (document.selection)
    		{
    			var str = document.selection.createRange().text;
    			v.focus();
    			var sel = document.selection.createRange();
    			sel.text = "[" + bbcode + "]" + str + "[/" + bbcode + "]";
    			return;
    		}
    		else if ((typeof v.selectionStart) != 'undefined') 
    		{
    			var txtarea = v;
    			var selLength = txtarea.textLength;
    			var selStart = txtarea.selectionStart;
    			var selEnd = txtarea.selectionEnd;
    			var s1 = (txtarea.value).substring(0,selStart);
    			var s2 = (txtarea.value).substring(selStart, selEnd)
    			var s3 = (txtarea.value).substring(selEnd, selLength);
    			txtarea.value = s1 + '[' + bbcode + ']' + s2 + '[/' + bbcode + ']' + s3;
    			txtarea.selectionStart = s1.length;
    			txtarea.selectionEnd = s1.length + 5 + s2.length + v.length * 2;
    			return;
    		}
    		else 
    		{
    
    		}
    	}
    	catch(e){
    	}
    }
    
    function insert_bbtext(v, bbtext)
    {
    	try {
    		if (document.selection)
    		{
    			var str = document.selection.createRange().text;
    			v.focus();
    			var sel = document.selection.createRange();
    			sel.text = bbtext;
    			return;
    		}
    		else if ((typeof v.selectionStart) != 'undefined') 
    		{
    			var txtarea = v;
    			var selLength = txtarea.textLength;
    			var selStart = txtarea.selectionStart;
    			var selEnd = txtarea.selectionEnd;
    			var s1 = (txtarea.value).substring(0,selStart);
    			var s2 = (txtarea.value).substring(selStart, selEnd)
    			var s3 = (txtarea.value).substring(selEnd, selLength);
    			txtarea.value = s1 + bbtext + s2 + s3;
    			txtarea.selectionStart = s1.length;
    			txtarea.selectionEnd = s1.length + 5 + s2.length + v.length * 2;
    			return;
    		}
    		else 
    		{
    
    		}
    	}
    	catch(e){
    	}
    }
    
    // ]]>
        </script>
    
    </head>
    <body>
        <form id="xxx" action="?">
            <textarea id="message" cols="40" rows="20">Roma
    Pordenone
    Milano</textarea>
            <select id="Select1">
                <option value="Stefano">Stefano</option>
                <option value="Carlo">Carlo</option>
                <option value="Nicola">Nicola</option>
            </select>
            
    
            seleziona un testo e inserisci bbcode
            
    
            Metti il cursore nel punto ove vuoi inserire il valore del dropdown
        </form>
    </body>
    </html>
    Pietro

  6. #6
    Utente di HTML.it L'avatar di otello
    Registrato dal
    May 2003
    Messaggi
    296
    codice:
    Originariamente inviato da pietro09 
    function insert_bbtext(v, bbtext)
    {
    			if (document.selection)
    		{
    			var str = document.selection.createRange().text;
    			v.focus();
    			var sel = document.selection.createRange();
    			sel.text = bbtext;
    			return;
    		}
    		else if ((typeof v.selectionStart) != 'undefined') 
    		{
    			var txtarea = v;
    			var selLength = txtarea.textLength;
    			var selStart = txtarea.selectionStart;
    			var selEnd = txtarea.selectionEnd;
    			var s1 = (txtarea.value).substring(0,selStart);
    			var s2 = (txtarea.value).substring(selStart, selEnd)
    			var s3 = (txtarea.value).substring(selEnd, selLength);
    			txtarea.value = s1 + bbtext + s2 + s3;
    			txtarea.selectionStart = s1.length;
    			txtarea.selectionEnd = s1.length + 5 + s2.length + v.length * 2;
    			return;
    		}
    }
    Ad occhio e croce dovrebbe essere quello che cercavo, solo che non riesco a farlo funzionare, dando per scontata la mia ignoranza, mi spieghi con un po' di pazienza cosa fa nel pezzo quotato? In particolare cosa sono "str" e "sel"?
    Grazie

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    carissimo otello, è la prima volta che provo una cosa di tal genere.
    Ho fatto una pagina html, l'ho provata con IE6 e Firefox 1.5 it, ho visto che funziona e te l'ho spedita.
    Accidenti, ho messo una TextArea, un DropDown, un Button, e mi vieni a dire che non ci riesci a farlo funzionare?

    -2 punti dalla patente

    Pietro

  8. #8
    Il problema è che in questa discussione si è partiti con il parlare di un argomento OT per questo forum.

    Ho l'obbligo di chiudere questa discussione. Otello, per qualsiasi altro problema in merito prova a dare un'occhiata sul forum Javascript.


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.