[Javascript] Hidden fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    [Javascript] Hidden fields

    [Javascript] Hidden field

    Hello my friends.

    This is a htm page that contains a form open in window popUp ( page daughter ).

    This form insert a value in the one hidden field called "tec", in other form open in the page mother.

    As you can see the select contains multiple attribute.

    The problem is that by selecting more names from select tec form page daughter, in the hidden field called "tec" form page mother I have only the first name, for example:

    I select value gabriele and giuseppe form page daughter, in the hidden field called "tec" form page mother I have only gabriele.

    Why?
    Can you help me ???



    Code:
    <script language="Javascript">
    
    <!--
    
    function insertf1(f1) {
       
       if ( FORM1.tec.value.length > 0 && FORM1.opr.value.length > 0 ) {
            window.opener.document.FORM1.tec.value=f1;
            alert("Dati correttamente salvati.");
            window.close();
    
        } else {
        
            alert("Dati obbligatori."); 
            FORM1.tec.focus(); 
    
       }
       
    }
       
    // -->
    </script>
    </head>
    
    <body>
    
    <form name="FORM1">
    
    <select size="15" name="tec" multiple>
    
    <option>Seleziona</option>
    <option value="GABRIELE">GABRIELE</option>                   
    <option value="GIUSEPPE">GIUSEPPE</option>
    <option value="DOMENICO">DOMENICO</option>
    
    </select> 
    
    
    <select size="15" name="opr" multiple>
    <option>Seleziona</option>
    <option value="GIOVANNI">GIOVANNI/option>                   
    <option value="GIACOMO">GIACOMO</option>
    <option value="DOROTEO">DOROTEO</option>
    
    </select>
    
    
    <a href="javascript:insertf1(document.FORM1.tec.value+';'+document.FORM1.opr.value);">            
    <img border="0" src="salva.gif" width="84" height="16">
    This is the hidden field called "tec" form page mother:

    Code:
    <INPUT TYPE="hidden" NAME="tec" VALUE="-;-">
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    For a multiple select element, you need to loop over the options and check which ones are selected.

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      Originally posted by acoder
      For a multiple select element, you need to loop over the options and check which ones are selected.
      Hi acoder, thanks x your reply.

      I write this for your suggestion:

      Code:
          function options_value_selezionati_join(lista)
          {
      	    var s = "";
      	    for(var i = 0; i < lista.options.length; i++)
      	    
      	    {
      		    if(lista.options[i].selected) s += "-" + lista.options[i].value;  
                
      	    }
      	    
      	    return s.substr(1);
      	    
         }
      
      ...
      
                  <a href="javascript:(options_value_selezionati_join(document.FORM1.tec)+';'+options_value_selezionati_join(document.FORM1.opr));">

      The values of select it's correct.... but not open alert "OK" and not close window popup and not insert a value in the hidden field called "tec" in form open in the page mother.
      .... :(


      Can you help me?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        All you've done is constructed a string without setting it to anything.

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Originally posted by acoder
          All you've done is constructed a string without setting it to anything.
          Not working...

          Code:
          function options_value_selezionati_join(lista)
              {
          	    
          	    var s = "";
          	    for(var i = 0; i < lista.options.length; i++)
          	    
          	    {
          
          		    if(lista.options[i].selected) s += "-" + lista.options[i].value; 
          		    
          
          		    	    
                    
          	    }	    
          	    
          	            window.opener.document.options_value_selezionati_join(document.FORM1.tec)=lista;
          
                 
             
          	    
             }
          
          ...
          
          <a href="javascript:options_value_selezionati_join(document.FORM1.tec); window.close();">

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Try:
            [code=javascript]window.opener.d ocument.FORM1.t ec.value = s;[/code]Can you see where you were going wrong?

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Originally posted by acoder
              Try:
              [code=javascript]window.opener.d ocument.FORM1.t ec.value = s;[/code]Can you see where you were going wrong?
              Code:
              function options_value_selezionati_join(lista)
                  {
              	    
              	    var s = "";
              	    for(var i = 0; i < lista.options.length; i++)
              	    
              	    {
              
              		    if(lista.options[i].selected) s += "-" + lista.options[i].value;
              
                	    }         	    
              		        window.opener.document.FORM1.tec.value = s;   
                              alert("OK.");
                              window.close();  
              	               
                 
              	    
                 }
              thanks, working but:


              Code:
               
              
              <a href="javascript:options_value_selezionati_join(document.FORM1.tec+';'+document.FORM1.opr);">
              Response whit 'options.lengh' is null or not object... why?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                The function is expecting a list/select object, not a string, so pass in one select reference.

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  Originally posted by acoder
                  The function is expecting a list/select object, not a string, so pass in one select reference.
                  I need pass this string:

                  <a href="javascrip t:options_value _selezionati_jo in(document.FOR M1.tec+';'+docu ment.FORM1.opr) ">

                  Any idea?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Pass two lists instead of a string and then loop over both and concatenate the string inside the function.

                    Comment

                    • viki1967
                      Contributor
                      • Oct 2007
                      • 263

                      #11
                      Originally posted by acoder
                      Pass two lists instead of a string and then loop over both and concatenate the string inside the function.
                      thanks now working, this is solution:

                      Code:
                      <script language="Javascript">
                      
                      <!--
                      
                          function options_value_selezionati_join(lista)
                          {
                      	    var s = "";
                      	    for(var i = 0; i < lista.options.length; i++)
                      	    {
                      		    if(lista.options[i].selected) s += "," + lista.options[i].value;
                      	    }
                      	    return s.substr(1);
                          }
                      
                          
                      function go()
                      {
                          var s1 = options_value_selezionati_join(document.FORM1.tec)
                          var s2 = options_value_selezionati_join(document.FORM1.opr);
                          
                          if(s1 == "" || s2 == "")
                          {
                              alert("Stop!");
                              return;
                          }
                          
                          var s = s1 + ";" + s2;
                      
                          if(window.opener && window.opener.document.FORM1.tec) window.opener.document.FORM1.tec.value = s;  
                          alert("OK."); 
                          window.close();
                      }    
                      
                         
                      // -->
                      </script>
                      
                      ...
                      
                      <a href="#" onclick="go();return false;">
                                  <img border="0" src="save.gif" width="84" height="16"></a>

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Yep, much better. Glad you got it working.

                        Comment

                        Working...