By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

[Javascript] Problem with select value

100+
P: 263
Hello everyone.

Try this htm page, please:
http://users1.titanichost.com/MiguelRivero61/

This is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3.  
  4. <head>
  5.  
  6. <script language="javascript" type="text/javascript">
  7. <!--
  8.  
  9. function Popup(larg,alte,url) 
  10. var w = screen.width; 
  11. var h = screen.height; 
  12. var x = Math.round(w / 2) - Math.round(larg / 2); 
  13. var y = Math.round(h / 2) - Math.round(alte / 2); 
  14. window.open(url,'','width='+larg+',height='+alte+',top='+y+',left='+x+''); 
  15.  
  16.  
  17. //-->
  18. </script> 
  19.  
  20. </head>
  21.  
  22. <body>
  23.  
  24. <form name="myform" action="">
  25.  
  26. <input name="data_ispezione" size="20">
  27.  
  28. <select class=blub size="1" name="Esito_ispezione"    
  29.        onchange="var cc = this.options[this.selectedIndex].value; 
  30.        if(cc=='2' && document.myform.data_ispezione.value!='')
  31.        {Popup(600,200,'Upload_db.asp')}else{alert('Inserire una data!')};">
  32.  
  33.       <option>Seleziona</option>
  34.       <option value="1">Si</option>
  35.       <option value="2">No</option>
  36.       </select>
  37.  
  38.    </form>   
  39. </body>
  40.  
  41. </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
Nov 20 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
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?
Nov 20 '08 #2

100+
P: 263
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.
Nov 20 '08 #3

acoder
Expert Mod 15k+
P: 16,027
To satisfy condition 2, you'll need to change the 'else' statement into an 'else if' statement, e.g.
Expand|Select|Wrap|Line Numbers
  1. else if(cc=='1' && document.myform.data_ispezione.value=='') {
  2. alert('...');
  3. }
By the way, it may be better to put the onchange code into a separate function to make things easier.
Nov 20 '08 #4

100+
P: 263
To satisfy condition 2, you'll need to change the 'else' statement into an 'else if' statement, e.g.
Expand|Select|Wrap|Line Numbers
  1. else if(cc=='1' && document.myform.data_ispezione.value=='') {
  2. alert('...');
  3. }
By the way, it may be better to put the onchange code into a separate function to make things easier.
Sorry but not working...

Expand|Select|Wrap|Line Numbers
  1. 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...
Nov 20 '08 #5

acoder
Expert Mod 15k+
P: 16,027
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.
Expand|Select|Wrap|Line Numbers
  1. 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')};">
Nov 20 '08 #6

100+
P: 263
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.
Expand|Select|Wrap|Line Numbers
  1. 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
Nov 20 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Glad it's working. Sometimes you have to rethink what you're doing to make solving a problem a little easier.
Nov 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.