Check with JavaScript a Select Multiple field if it has value

0

I have two multiple select's as if they were a De > To, in the "From" I have email list coming from bank with Classic ASP, I can already pass the emails to the select "To" but I can not get the emails in the transport function, I would like the help of vcs if you can use a way to get those values that are in the select To, follow the code below with the whole process.

<HTML>
<HEAD>
    <TITLE>Mailing</TITLE>
    <link rel="stylesheet" href="../../../Estilos/estilos.css" type="text/css">
    <script src="../../../JS/functions.js"></script>
    <script language="JavaScript">
        function Transporte() {

            var retEmails;
            var objSelecionados = document.frmSelecionaEmail.Selec.value;

            alert("Pegou os e-mails");
            alert(objSelecionados);

            if (objSelecionados != '') {

                for (i = 0; i <= objSelecionados.options.length; i++) {

                    if (objSelecionados.options[i].selected = true) {
                        retEmails = objSelecionados[i].value;
                    }
                }
            } else {
                alert("Nenhum e-mail selecionado.");
            }

            alert(retEmails);

        }
    </script>
</HEAD>
<BODY topmargin="5" leftmargin="0">

    <form name="frmSelecionaEmail" method="post" action="">
        <!--CORPO-->
        <td width="2%" class="subInfoTit"><input type="submit" name="btOk" value="Ok" class="cmd" onclick="return Transporte();" /></td>
        <table width="100%" border="0" cellpadding="2" cellspacing="0" class="tbllogin">
            <tr>
                <td class="subInfoTit" colspan="2">
                <%if Session("SGALinguagem") = "PT" then %>
                    Lista por E-mail
                <%elseif Session("SGALinguagem") = "ES" then %>
                    Lista por Correo elecrónico
                <%elseif Session("SGALinguagem") = "EN" then %>
                    List by E-mail
                <%end if%>                  
                </td>
            </tr>
            <tr height="21">
                <td colspan="7" width="100%">
                    <table border="0" cellpadding="2" cellspacing="2" width="100%">
                        <tr>
                            <td align="center" width="45%"><b>
                            <%if Session("SGALinguagem") = "PT" then %>
                                E-mails selecionados
                            <%elseif Session("SGALinguagem") = "ES" then %>
                                Correo electrónico no seleccionados
                            <%elseif Session("SGALinguagem") = "EN" then %>
                                Unselected E-mails
                            <%end if%>
                            </b></td>
                            <td align="center" width="10%"></td>
                            <td align="center" width="45%"><b>
                            <%if Session("SGALinguagem") = "PT" then %>
                                E-mails não selecionados
                            <%elseif Session("SGALinguagem") = "ES" then %>
                                Correo electrónico seleccionados
                            <%elseif Session("SGALinguagem") = "EN" then %>
                                Selected E-mails
                            <%end if%>
                            </b></td>
                        </tr>
                        <tr>
                            <td align="center" width="45%">                         
                                <select name="NSelec" size="8" multiple style="width: 100%;">
                                <%
                                SQL="SELECT Email_Endereco, Email_Nome FROM Email"

                                    Call AbreRS(SQL)
                                    While Not Rs.EOF
                                        'v_Id_Email          = Rs("Id_Email")
                                        'v_Email_Nome        = Rs("Email_Nome")
                                        v_Email_Endereco    = Rs("Email_Endereco")

                                        %>
                                        <option value="<%=v_Email_Endereco%>"><%=v_Email_Endereco%></option>
                                        <%
                                        Rs.MoveNext
                                    Wend
                                    Call FechaRS
                                %>
                                </select>                                   
                            </td>                         
                            <td align="center" width="10%">
                                <input type="button" value=">" name="Ins_Item" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('Selec', 'NSelec', false);"><br>
                                <input type="button" value=">>" name="Ins_Todos" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('Selec', 'NSelec', true);"><br>
                                <input type="button" value="<<" name="Ret_Todos" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('NSelec', 'Selec', true);"><br>
                                <input type="button" value="<" name="Ret_Item" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('NSelec', 'Selec', false);">
                            </td>
                            <td align="center" width="45%">
                                <select name="Selec" size="8" multiple style="width: 100%;">

                                </select>
                            </td>

                        </tr>
                    </table>
                </td>
            </tr>

        </table>
    </form>
</BODY>
</HTML>
    
asked by anonymous 21.07.2018 / 00:08

1 answer

0

You have some errors in your code, such as:

i <= objSelecionados.options.length

Using the <= operator will extrapolate the number of elements, resulting in an error. The correct is to use only < to iterate to the limit of the array ( the maximum index of an array is equal to the number of items -1 ):

i < objSelecionados.options.length

Another error is the assignment operator = in if . The correct operator is == .

One detail is that by selecting select with document.frmSelecionaEmail.Selec % is already returned, so you do not need to use option .

I changed the code by transforming the options[i] variable into array so that the selected emails are stored.

In the example below I made changes to the HTML for illustration only (see explanations in the code):

function Transporte() {

   var retEmails = []; // cria a array
   var objSelect = document.frmSelecionaEmail.Selec; // seleciona o select

   if (objSelect.value) { // verifica se há ao menos 1 item selecionado

      for (i = 0; i < objSelect.length; i++) {
         if (objSelect[i].selected == true) {
            retEmails.push(objSelect[i].value); // adiciona na array apenas os itens selecionados
         }
      }
   } else {
      alert("Nenhum e-mail selecionado.");
   }

   console.clear();
   console.log(retEmails);

}
<form name="frmSelecionaEmail" method="post" action="">
     <!--CORPO-->
     <td width="2%" class="subInfoTit"><input type="button" name="btOk" value="Ok" class="cmd" onclick="return Transporte();" /></td>
     <table width="100%" border="0" cellpadding="2" cellspacing="0" class="tbllogin">
         <tr height="21">
             <td colspan="7" width="100%">
                 <table border="0" cellpadding="2" cellspacing="2" width="100%">
                     <tr>
                         <td align="center" width="45%">                         
                             <select name="NSelec" size="8" multiple style="width: 100%;">
                             </select>                                   
                         </td>                         
                         <td align="center" width="10%">
                             <input type="button" value=">" name="Ins_Item" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('Selec', 'NSelec', false);"><br>
                             <input type="button" value=">>" name="Ins_Todos" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('Selec', 'NSelec', true);"><br>
                             <input type="button" value="<<" name="Ret_Todos" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('NSelec', 'Selec', true);"><br>
                             <input type="button" value="<" name="Ret_Item" style="border: 1px solid #636396;color: #636396;width:30px;cursor:hand;" OnClick="MoveDadosLista('NSelec', 'Selec', false);">
                         </td>
                         <td align="center" width="45%">
                             <select name="Selec" size="8" multiple style="width: 100%;">
                                  <option value="[email protected]">[email protected]</option>
                                  <option value="[email protected]">[email protected]</option>
                                  <option value="[email protected]">[email protected]</option>

                             </select>
                         </td>

                     </tr>
                 </table>
             </td>
         </tr>

     </table>
 </form>
    
21.07.2018 / 00:37