Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
Mover lista de selección múltiple con JavaScript

Estoy programando por primera vez. Quiero escribir un código que mueva varias selecciones de una lista con los botones arriba y abajo en JavaScript. Sin embargo, pude escribir algo que se mueve hacia arriba y hacia abajo, pero no funciona bien si me muevo varias veces. Dígame cómo mover varios elementos seleccionados hacia arriba y hacia abajo.

Si selecciona 3 y 5 y presiona el botón arriba, desea hacer lo siguiente.

 <option value = "1"> 1 </ option> <option value = "2"> 2 </ option> <option value = "3"> 3 </ option> <option value = "4"> 4 </ option> <option value = "5"> 5 </ option> <option value = "6"> 6 </ option> ↓ <option value = "1"> 1 </ option> <option value = "3"> 3 </ option> <option value = "2"> 2 </ option> <option value = "5"> 5 </ option> <option value = "4"> 4 </ option> <option value = "6"> 6 </ option>
 <table> <tr> <td> <select id="item-list" size="8" multiple="multiple"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </td> <td> <input type="button" value="↑" onclick="move('up');"> <br/> <input type="button" value="↓" onclick="move('down');"> </td> </tr> </table>
 function move(act) { var s = document.getElementById("item-list"); if (s.selectedIndex == -1) return; var opt = s.options[s.selectedIndex]; if (act == 'up') { if (s.options[s.selectedIndex - 1]) { s.insertBefore(opt, s.options[s.selectedIndex - 1]); } } if (act == 'down') { if (s.options[s.selectedIndex + 1]) { s.insertBefore(opt, s.options[s.selectedIndex + 1].nextSibling); } } }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Su código tenía varios errores pequeños. lo arreglé

 function move(act) { var s = document.getElementById("item-list"); if(s.selectedIndex == -1) return; var opt = s.options[s.selectedIndex]; var opts = s.options; for (let i = 0; i < opts.length; i++) { if (opts[i].selected){ console.log(opts[i].value); if (act == 'up') { if (s.options[s.selectedIndex-1]) { s.insertBefore(opt, s.options[s.selectedIndex-1]); } } if (act == 'down') { if (s.options[s.selectedIndex+1]) { s.insertBefore(opt, s.options[s.selectedIndex+1].nextSibling); } } } } }
 <table> <tr> <td> <select id="item-list" size="8" multiple="multiple"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </td> <td> <input type="button" value="↑" onclick="move('up');"> <br/> <input type="button" value="↓" onclick="move('down');"> </td> </tr> </table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda