Link to home
Start Free TrialLog in
Avatar of smphil
smphilFlag for Afghanistan

asked on

How do you disable a select menu

How can I disable the country select menu if anything but USA is chosen?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Select Menu Help</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="">
  <select name="country" id="country">
    <option>Country</option>
    <option value="USA">USA</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
  </select>
  <select name="state" id="state">
  </select>
  <select name="sex" id="sex">
  </select>
</form>
</body>
</html>
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Select Menu Help</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="">
  <select name="country" id="country" onchange="if (this.options[this.selectedIndex].value!='USA') this.disabled=true">
    <option>Country</option>
    <option value="USA">USA</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
  </select>
  <select name="state" id="state">
  </select>
  <select name="sex" id="sex">
  </select>
</form>
</body>
</html>


Cd&
If you want to disable All other countries other than USA, what'd be the purpose of displaying All other countries!?

If you meant disabling "state" if other coutry is selected, you could use this:

onchange="if (this.options[this.selectedIndex].value!='USA') this.form.state.disabled=true; else this.form.state.disabled=false;"
Avatar of smphil

ASKER

Thats it jags if any other country but usa is selected I want the state disabled
Avatar of smphil

ASKER

What happens to the value of the state when its disabled can I have it default to say "Not In the USA"
So say they screw with the state first then choose a different coutry can it be disabled to a default like "Not IN USA"

Avatar of arun99907
arun99907

When you disable a <select name="state"> menu and submit the page, and in the next page if you try to retrieve it with request.getParameter("state"); it will not give you any value.

The first page might show you some default option value "Select State" in the select state drop down [though disabled], it will not be passed to the next page. You will always end up getting "null" value for request.getParameter("state");

If you want it to pass a default value "NotInUSA" you have no other go but to enable the state with the option value set to "NotInUSA" and submit it. i.e., just onClick of submit button, call a javascript and write the code to enable the state select menu and submit the page. This way user will feel for a glance of minute before submitting the value selected as "NotInUSA".

jsp1.jsp
-----
<html>
 <head>
   <script language="JavaScript">
    function submit()
    {
       if(document.jsp1form.state.disabled==true)
         {
            document.jsp1form.state.disabled = false;
            document.jsp1form.state.options[this.selectedIndex].value = 'NotInUSA';
         }
        document.jsp1form.src = "jsp2page.jsp";
        document.jsp1form.submit();
    }
   </script>
 </head>
  <body>
    <form name=jsp1form>
        <select name="country" id="country" onchange="if (this.options[this.selectedIndex].value!='USA') this.form.state.disabled=true; else this.form.state.disabled=false;">
    <option>Country</option>
    <option value="USA">USA</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
    <option value="All other countries">All other countries</option>
  </select>
  <select name="state" id="state">
  </select>
  <select name="sex" id="sex">
  </select>
  <input type=button value="submit" onClick="javascript:submit()">
    </form>
  </body>
</html>

Hope this should answer !!

(Tip: just onclick of submitting, enable the state that is disabled to send it as a parameter, other wise it wont be passed to the next page)
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial