Afficher la sélection d'une liste à choix multiple dans une autre liste
Bonjour,
j'ai recherché si mon problème avait déjà été posé, et je n'ai pas trouvé. Bref.
J'ai une liste à choix multiple remplie avec ma base de données.
Quand on sélectionne un élément "elem1" dans la liste, et que l'on appuie sur un bouton "ajout" à côté, "elem1" doit s'afficher dans la seconde liste à choix multiple.
J'ai trouvé comment faire avec des valeurs ne provenant pas de la base de données, mais mélanger du javascript avec des listes remplies grâce à PHP/MySQL, je ne vois pas bien comment faire ...
Voici ce que j'ai comme code pour le moment (trouvé sur le web) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <script language="JavaScript">
function moveItem1To2 () {
var i1 = document.testForm.list1.selectedIndex;
var text1 = document.testForm.list1.options[i1].text;
var value1 = document.testForm.list1.options[i1].value;
var list2 = document.testForm.list2;
list2.options[list2.options.length]= new Option(text1 ,value1);
document.testForm.list1.remove(i1);
}
function moveItem2To1 () {
var text1 = document.testForm.list2.options[document.testForm.list2.selectedIndex].text;
var value1 = document.testForm.list2.options[document.testForm.list2.selectedIndex].value;
var list1 = document.testForm.list1;
list1.options[list1.options.length]= new Option(text1,value1);
document.testForm.list2.remove(document.testForm.list2.selectedIndex);
}
</script>
<body>
<form name="testForm">
<select name="list1" onChange="moveItem1To2()" size=5>
<script language="javascript">
var list1 = document.testForm.list1;
list1.options[list1.options.length] = new Option('Pomme','P1');
list1.options[list1.options.length] = new Option('Poire','P2');
list1.options[list1.options.length] = new Option('Peche','P3');
list1.options[list1.options.length] = new Option('Poivre','P4');
list1.options[list1.options.length] = new Option('Prune','P5');
list1.options[list1.options.length] = new Option('Abricot','A1');
</script>
</select>
<select name="list2" onChange="moveItem2To1()" size=5>
<option value="1">Pomme
</select>
</form> |
Y'a-t-il un moyen de remplacer "pomme, poire, pêche ..." par les données de ma table ?