Check with Javascript a Select Multiple field if it has value

Asked

Viewed 723 times

0

I have two select’s Multiple as if they were a From > To, in "From" I have mailing list coming from bank with ASP Classic, I can already pass the emails to the select "To", but I can’t get the emails in the transport function, I would like the help of you if you can use a way to get these values that are in select.

<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>
  • I know the "selects" field is empty at the end, but is there any way to get him to check it out? Because this screen there is a popup that will send the emails to the main screen in a field to my mailing system, thank you.

1 answer

0

There are some errors in your code, for example:

i <= objSelecionados.options.length

Using the operator <= will extrapolate the number of elements resulting in error. 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 allocation operator = in the if. The correct operator is ==.

One detail is that selecting the select with document.frmSelecionaEmail.Selec is already returned the options, so you don’t need to use options[i].

I changed the code by transforming the variable retEmails in array so that the selected emails are stored.

In the example below I made changes to HTML just to illustrate how it works (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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.