[Javascript] Problem with select value

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

    [Javascript] Problem with select value

    Hello everyone.

    Try this htm page, please:


    This is the code:

    Code:
     
    <html>
      
    <head>
      
    <script language="javascript" type="text/javascript">
    <!--
     
    function Popup(larg,alte,url) 
    { 
    var w = screen.width; 
    var h = screen.height; 
    var x = Math.round(w / 2) - Math.round(larg / 2); 
    var y = Math.round(h / 2) - Math.round(alte / 2); 
    window.open(url,'','width='+larg+',height='+alte+',top='+y+',left='+x+''); 
    } 
    
     
    //-->
    </script> 
     
    </head>
     
    <body>
     
    <form name="myform" action="">
     
    <input name="data_ispezione" size="20">
     
    <select class=blub size="1" name="Esito_ispezione"    
           onchange="var cc = this.options[this.selectedIndex].value; 
           if(cc=='2' && document.myform.data_ispezione.value!='')
           {Popup(600,200,'Upload_db.asp')}else{alert('Inserire una data!')};">
          
          <option>Seleziona</option>
          <option value="1">Si</option>
          <option value="2">No</option>
          </select>
          
       </form>   
    </body>
     
    </html>
    If the fields data_ispezione is empty and selected value "No" (2) in the select Esito_ispezione , should not see and open page Upload_db.asp.

    Now all values selected in the select Esito_ispezione open the alert, why?

    Gracias
    MR
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If I understand correctly, you don't want to open the pop-up when the select value is "2"/No or if no text has been input. Is that correct?

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      Originally posted by acoder
      If I understand correctly, you don't want to open the pop-up when the select value is "2"/No or if no text has been input. Is that correct?
      1) If field data_ispezione is empty and selected value "No" (2) in the select Esito_ispezione NOT open pop-up and OPEN alert "data empty";

      2) If field data_ispezione is empty and selected value "Si" (1) in the select Esito_ispezione alert "data empty";

      3) If field data_ispezione NOT is empty and selected value "No" (2) in the select Esito_ispezione OPEN pop-up;

      thanks x your reply.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        To satisfy condition 2, you'll need to change the 'else' statement into an 'else if' statement, e.g.
        Code:
        else if(cc=='1' && document.myform.data_ispezione.value=='') {
        alert('...');
        }
        By the way, it may be better to put the onchange code into a separate function to make things easier.

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Originally posted by acoder
          To satisfy condition 2, you'll need to change the 'else' statement into an 'else if' statement, e.g.
          Code:
          else if(cc=='1' && document.myform.data_ispezione.value=='') {
          alert('...');
          }
          By the way, it may be better to put the onchange code into a separate function to make things easier.
          Sorry but not working...

          Code:
          onchange="var cc = this.options[this.selectedIndex].value; else if(cc=='2' && document.myform.data_ispezione.value!=''){Popup(600,200,'Upload_db.asp')}else{this.selectedIndex=0; alert('Inserire una data!')};">
          1) Field data empty: selected value 1 or value 2 in the select open the alert: IT'S OK

          2) Field data NOT empty: selected value 2 in the select OPEN pop-up; IT'S OK

          3) Field data NOT empty: selected value 1 in the select open the alert; IT'S WRONG because Field data is NOT empty...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You haven't added the if statement to the else, but anyway what you should really be doing is checking the input field first, e.g.
            Code:
            onchange="var cc = this.options[this.selectedIndex].value; if(document.myform.data_ispezione.value=='') {this.selectedIndex=0; alert('Inserire una data!')} else if (cc=='2') {Popup(600,200,'Upload_db.asp')};">

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Originally posted by acoder
              You haven't added the if statement to the else, but anyway what you should really be doing is checking the input field first, e.g.
              Code:
              onchange="var cc = this.options[this.selectedIndex].value; if(document.myform.data_ispezione.value=='') {this.selectedIndex=0; alert('Inserire una data!')} else if (cc=='2') {Popup(600,200,'Upload_db.asp')};">
              thanks genius !!!!

              Bye
              Viki

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Glad it's working. Sometimes you have to rethink what you're doing to make solving a problem a little easier.

                Comment

                Working...