Visualizzazione dei risultati da 1 a 4 su 4

Discussione: eliminare valori doppi

  1. #1

    eliminare valori doppi

    ciao a tutti,
    avrei necessità di un aiuto per un problema su un campo.

    In pratica ho creato in una pagina un campo
    <input name="category" size="30" type="text" value="" class="stile" id="category">

    In questo campo vado a concatenare, subito dopo il submit, tutti i valori delle checkbox che vengono selezionati.
    var category = old_category + '; ' + category;
    document.getElementById("category").value = category;

    ottengo questo risultato : campo_category = <valore1>;<valore2>;<valore3>....<valoren>;

    Se torno indietro o riapro in modifica la pagina, vedo che le checkbox non sono selezionate e il campo è pieno dei valori precedentemente inseriti.

    Non appena riseleziono le checkbox, mi mette i valori doppi. La gestione delle checkbox cosi, mi adrebbe pure bene, vorrei trovare una soluzione per eliminare i valori doppi.

    Qualche suggerimento?

    Grazie

    Ciao

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,132
    Nella funzione che inserisce i checkbox selezionati forza prima lo svuotamento del campo category
    document.getElementById("category").value = "";
    var category = old_category + '; ' + category;
    document.getElementById("category").value = category;
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    ciao,
    purtroppo ho provato ma non ha funzionato.
    Posto qui sotto tutto il codice(ho messo qualche commento di spiegazione). Spero possa aiutare a capire meglio il problema.
    <script type="text/javascript">
    if ( (event == 'save') || (event == 'authorpreview')) {

    //Qui sotto il controllo di tutte le mie checkbox, siccome sono facoltative, anche se nessuna è selezionata nn faccio niente
    if (document.getElementById("category04").checked == false &amp;&amp;
    document.getElementById("category06").checked == false &amp;&amp;
    document.getElementById("category08").checked == false &amp;&amp;
    document.getElementById("category10").checked == false &amp;&amp;
    document.getElementById("category12").checked == false &amp;&amp;
    document.getElementById("category14").checked == false &amp;&amp;
    document.getElementById("category16").checked == false &amp;&amp;
    document.getElementById("category18").checked == false &amp;&amp;
    document.getElementById("category20").checked == false &amp;&amp;
    document.getElementById("category22").checked == false &amp;&amp;
    document.getElementById("category24").checked == false &amp;&amp;
    document.getElementById("category26").checked == false &amp;&amp;
    document.getElementById("category28").checked == false &amp;&amp;
    document.getElementById("category30").checked == false &amp;&amp;
    document.getElementById("category32").checked == false
    )

    {

    } else { //Se qualcuna è selezionata, prendo il valore e lo metto dentro una variabile temporanea

    if ( document.getElementById("category04").checked == true)
    {var check_label1 = document.getElementById("category04").value;}
    if ( document.getElementById("category06").checked == true)
    {var check_label2 = document.getElementById("category06").value;}
    if ( document.getElementById("category08").checked == true)
    {var check_label3 = document.getElementById("category08").value;}
    if ( document.getElementById("category10").checked == true)
    {var check_label4 = document.getElementById("category10").value;}
    if ( document.getElementById("category12").checked == true)
    {var check_label5 = document.getElementById("category12").value;}
    if ( document.getElementById("category14").checked == true)
    {var check_label6 = document.getElementById("category14").value;}
    if ( document.getElementById("category16").checked == true)
    {var check_label7 = document.getElementById("category16").value;}
    if ( document.getElementById("category18").checked == true)
    {var check_label8 = document.getElementById("category18").value;}
    if ( document.getElementById("category20").checked == true)
    {var check_label9 = document.getElementById("category20").value;}
    if ( document.getElementById("category22").checked == true)
    {var check_label10 = document.getElementById("category22").value;}
    if ( document.getElementById("category24").checked == true)
    {var check_label11 = document.getElementById("category24").value;}
    if ( document.getElementById("category26").checked == true)
    {var check_label12 = document.getElementById("category26").value;}
    if ( document.getElementById("category28").checked == true)
    {var check_label13 = document.getElementById("category28").value;}
    if ( document.getElementById("category30").checked == true)
    {var check_label14 = document.getElementById("category30").value;}
    if ( document.getElementById("category32").checked == true)
    {var check_label15 = document.getElementById("category32").value;}

    //Concateno le label delle checkbox selezionate
    var str_category = '';

    if (check_label1 != null){str_category = str_category+check_label1 + '; '}
    if (check_label2 != null){str_category = str_category+check_label2 + '; '}
    if (check_label3 != null){str_category = str_category+check_label3 + '; '}
    if (check_label4 != null){str_category = str_category+check_label4 + '; '}
    if (check_label5 != null){str_category = str_category+check_label5 + '; '}
    if (check_label6 != null){str_category = str_category+check_label6 + '; '}
    if (check_label7 != null){str_category = str_category+check_label7 + '; '}
    if (check_label8 != null){str_category = str_category+check_label8 + '; '}
    if (check_label9 != null){str_category = str_category+check_label9 + '; '}
    if (check_label10 != null){str_category = str_category+check_label10 + '; '}
    if (check_label11 != null){str_category = str_category+check_label11 + '; '}
    if (check_label12 != null){str_category = str_category+check_label12 + '; '}
    if (check_label13 != null){str_category = str_category+check_label13 + '; '}
    if (check_label14 != null){str_category = str_category+check_label14 + '; '}
    if (check_label15 != null){str_category = str_category+check_label15 + '; '}

    //Elimino ultimo separatore
    var lung_testo = str_category.length;
    var category = str_category.substring(0,lung_testo-2);


    //Valorizzo categoria
    if (document.getElementById("category35").value == '') {
    document.getElementById("category35").value = category;
    }else {
    var old_category = document.getElementById("category35").value;
    --> document.getElementById("category35").value = "";
    category = old_category + '; ' + category;
    document.getElementById("category35").value = category;
    }


    }//Chiusura dell else check selezionate

    }//Chiusura di check evento save


    Io volevo fare una soluzione tipo 2 cicli for annidati sull'array....

    ciao
    Grazie

  4. #4
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,132
    Troppo complesso, come non detto
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

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 © 2024 vBulletin Solutions, Inc. All rights reserved.