Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with input type radio and select name

Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#1: Aug 14 '09
Hi everyone.

My form page htm:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <script language="javascript">
  4.         function controlla_combo()
  5.         {
  6.             document.form1.Nacionalidad_text.disabled = document.form1.Nacionalidad[0].checked;
  7.             document.form1.Nacionalidad_text.disabled = document.form1.Nacionalidad[1].checked;
  8.         }
  9.     </script>
  10. </head>
  11. <body>
  12.     <form name="form1" action="insert.jsp" method=post> 
  13.     <table>
  14.         <tr>
  15.             <td><input type="radio" name="Nacionalidad" value="1" onclick="controlla_combo();" CHECKED></td>
  16.             <td><input type="radio" name="Nacionalidad" value="0" onclick="controlla_combo();"></td>
  17.             <td><input type="radio" name="Nacionalidad" value="2" onclick="controlla_combo();"></td>
  18.             <td>
  19.                 <select name="Nacionalidad_text" disabled>
  20.  
  21.     <OPTION VALUE="MX">México</OPTION>
  22.     <OPTION VALUE="IT">Italia</OPTION>
  23.     <OPTION VALUE="ES">España</OPTION>
  24.  
  25.                 </select>
  26.             </td>
  27.         </tr>
  28.     </table>
  29.     </form>
  30. </body>
  31. </html>
I need this:

1) If checked input type="radio" name="Nacionalidad" value="1" the select name="Nacionalidad_text" is DISABLE;

2) If checked input type="radio" name="Nacionalidad" value="0" the select name="Nacionalidad_text" is DISABLE;

3) If checked input type="radio" name="Nacionalidad" value="2" the select name="Nacionalidad_text" is ENABLE;


The function function controlla_combo() NOT working... :

1) If checked input type="radio" name="Nacionalidad" value="1" the select name="Nacionalidad_text" is ENABLE;

2) If checked input type="radio" name="Nacionalidad" value="0" the select name="Nacionalidad_text" is DISABLE;

3) If checked input type="radio" name="Nacionalidad" value="2" the select name="Nacionalidad_text" is ENABLE;

Can you help me ?

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Aug 14 '09

re: Problem with input type radio and select name


your approach to the function is totally wrong. if you want the select enabled, check which radio is checked. (this requires a condition to be tested, thus use if () )
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#3: Aug 15 '09

re: Problem with input type radio and select name


OK, thanks x your answer. This code working: :)

Expand|Select|Wrap|Line Numbers
  1. <html> 
  2.     <head> 
  3.     <title>prova</title> 
  4.     <script type='text/javascript'> 
  5.         function controlla(valore) 
  6.         { 
  7.             if(valore==2) 
  8.             { 
  9.                 document.mioform.Nazionalità_text.disabled=false; 
  10.             } 
  11.             else 
  12.             { 
  13.                 document.mioform.Nazionalità_text.disabled=true; 
  14.             } 
  15.         } 
  16.  
  17.  
  18.  
  19. function submIt(theform) 
  20.  
  21.             var selObj = document.mioform.Nazionalità_text; 
  22.             var selIndex = selObj.selectedIndex; 
  23.             if(selIndex==0  && document.mioform.Nazionalità_text.disabled==true) 
  24.             { 
  25.                 return true; 
  26.             } 
  27.             else if(selIndex ==0 && document.mioform.Nazionalità_text.disabled==false) 
  28.             { 
  29.                 alert("OBBLIGATORIO"); 
  30.                 mioform.Nazionalità_text.focus(); 
  31.                 return false; 
  32.             } 
  33.  
  34.  
  35.  
  36.   return(true);
  37. }        
  38.     </script> 
  39.     </head> 
  40.  
  41.     <body onload="controlla(0)"> 
  42.         <form id="mioform" name="mioform" method="post" onsubmit="return submIt(this);"> 
  43.  
  44.             A <input onclick="controlla(this.value);" type="radio" name="Nazionalità" value="1" CHECKED><br> 
  45.             B <input onclick="controlla(this.value);" type="radio" name="Nazionalità" value="0"><br> 
  46.             C <input onclick="controlla(this.value);" type="radio" name="Nazionalità" value="2"><br> 
  47.             <br><br> 
  48.             <SELECT NAME="Nazionalità_text" SIZE="1"> 
  49.             <OPTION VALUE="">---------------------------</OPTION> 
  50.             <OPTION VALUE="IT">Italiana</OPTION> 
  51.             </SELECT> 
  52.  
  53.             <input type="submit" value="Invia" name="B1">
  54.  
  55.         </form> 
  56.     </body> 
  57. </html>
  58.  
Reply


Similar JavaScript / Ajax / DHTML bytes