Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > JavaScript: Общие вопросы > Перекинуть элемент из одного списка в другой


Автор: GePo 19.4.2005, 10:18
Есть два списка(select), между ними две кнопки ">>","<<"(вправо, влево). По нажатию на соответвующую кнопку выделенынй элемент(если выделен) в соответвующем списке должен перемещаться в другой. Возможно ли это реализовать на JavaScript и как?

Автор: batigoal 19.4.2005, 12:06
Конечно, можно. Например, так:
Код

<html>
<head>
<script>
function moveRight()
{
 var first = document.getElementById("first");
 var second = document.getElementById("second");
 if(first.selectedIndex!=-1)
 {
   second.options[second.options.length] = new Option(first.options[first.selectedIndex].text);
   for(var i = first.selectedIndex; i<first.options.length-1; i++)
   {
     first.options[i].text = first.options[i+1].text;
   }
   first.options.length = first.options.length-1;
 }
}
function moveLeft()
{
 var first = document.getElementById("first");
 var second = document.getElementById("second");
 if(second.selectedIndex!=-1)
 {
   first.options[first.options.length] = new Option(second.options[second.selectedIndex].text);
   for(var i = second.selectedIndex; i<second.options.length-1; i++)
   {
     second.options[i].text = second.options[i+1].text;
   }
   second.options.length = second.options.length-1;
 }
}
</script>
</head>
 <body>
  <div align="center">
   <select id="first" size="7">
    <option>one</option>
    <option>two</option>
    <option>three</option>
   </select>
   <select id="second" size="7">
    <option>four</option>
    <option>five</option>
    <option>six</option>
   </select>
   <br>
   <input type="button" onClick="moveLeft()" value="<<">
   <input type="button" onClick="moveRight()" value=">>">
  </div>
 </body>
</html>


Учти, код упрощенный.

Автор: Aliance 19.4.2005, 14:26
Вот сам алгоритм динамической подмены значений в вып. списке:
http://forum.vingrad.ru/index.php?showtopic=41476&view=findpost&p=318902

Автор: GePo 19.4.2005, 17:44
Lamer George
спасибо, прямо в точку.

Автор: batigoal 19.4.2005, 17:57
Не за что smile . Но повторяю еще раз - для реального использования код надо доработать. Как минимум, учесть, что работать надо не только с options[i].text, но и с options[i].value.

Автор: Sardar 19.4.2005, 18:20
Можно еще проще:
Код
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
function moveRight() {
 var first = document.getElementById("first");
 var second = document.getElementById("second");
 if(first.selectedIndex!=-1)
   second.appendChild(first.removeChild(first.options[first.selectedIndex]));
}
function moveLeft(){
 var first = document.getElementById("first");
 var second = document.getElementById("second");
 if(second.selectedIndex!=-1)
   first.appendChild(second.removeChild(second.options[second.selectedIndex]));
}
</script>
</head>
 <body>
  <div align="center">
   <select id="first" size="7">
    <option>one</option>
    <option>two</option>
    <option>three</option>
   </select>
   <select id="second" size="7">
    <option>four</option>
    <option>five</option>
    <option>six</option>
   </select>
   <br>
   <input type="button" onClick="moveLeft()" value="<<">
   <input type="button" onClick="moveRight()" value=">>">
  </div>
 </body>
</html>

Автор: batigoal 19.4.2005, 18:27
Да и пограмотней заодно...

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)