Discussions

Web tier: servlets, JSP, Web frameworks: Struts : enable/disable a textfield with radio buttons

  1. Hello,

    I would like to have a form in struts that has radio buttons that enables and disables a text field in the same form when when you select the two different radio buttons.
    For example : we have two radiobuttons : radio1 and radio2 and different textfields among one is called text1.
    When you click radio1, you want to disable text1 and when you click radio2 you enable text1, so that the user can give some text in that textfield.
    I found a wroking way to do this, but it has a little problem, what I will explain after.

    JavaScript :

    function enableTextField() {
      var form = document.forms[0];
       form.text1.disabled = false;
    }

    function enableTextField() {
      var form = document.forms[0];
       form.text1.disabled = true;
    }

    in my jsppage :

    <html:radio property="propertyName" value="radio1" onclick="disableTimeFields()"></html:radio>

    <html:radio property="propertyName" value="radio2" onclick="enableTimeFields()"></html:radio>

    <html:text property="text1" disabled = "true"/>



    This works fine for enabling and disabling "text1", but gives problems in two cases :
    1)when you selected radio2 (and enabled the text field) and you submit and the validator detects a fault in one of the other text fields (let's say "text2", a textfield which has no dependence on the radio buttons and is always enabled) he returns the same form to correct your fault, but now "text1" is disabled again, even while radio2 is still selected!
    Of course I would still like to see that "text1" is enabled, because it was that way before i submitted the form.
    2)When I submit the form and afterwards I use the "back" button of my browser I have the same problem as above. Radio2 is still selected but my textfield "text1" is disabled!


    Does anybody knows a good solution for this problem????


    Thanx in advance!!!!

    Olivier Jennes
  2. Hello,I would like to have a form in struts that has radio buttons that enables and disables a text field in the same form when when you select the two different radio buttons.For example : we have two radiobuttons : radio1 and radio2 and different textfields among one is called text1.When you click radio1, you want to disable text1 and when you click radio2 you enable text1, so that the user can give some text in that textfield.I found a wroking way to do this, but it has a little problem, what I will explain after.JavaScript :function enableTextField() {&nbsp;&nbsp;var form = document.forms[0];&nbsp;&nbsp; form.text1.disabled = false;}function enableTextField() {&nbsp;&nbsp;var form = document.forms[0];&nbsp;&nbsp; form.text1.disabled = true;}in my jsppage :<html:radio property="propertyName" value="radio1" onclick="disableTimeFields()"></html:radio><html:radio property="propertyName" value="radio2" onclick="enableTimeFields()"></html:radio><html:text property="text1" disabled = "true"/>This works fine for enabling and disabling "text1", but gives problems in two cases :1)when you selected radio2 (and enabled the text field) and you submit and the validator detects a fault in one of the other text fields (let's say "text2", a textfield which has no dependence on the radio buttons and is always enabled) he returns the same form to correct your fault, but now "text1" is disabled again, even while radio2 is still selected!Of course I would still like to see that "text1" is enabled, because it was that way before i submitted the form.2)When I submit the form and afterwards I use the "back" button of my browser I have the same problem as above. Radio2 is still selected but my textfield "text1" is disabled!Does anybody knows a good solution for this problem????Thanx in advance!!!!Olivier Jennes

    The way that I have dealt with the first problem is to have a hidden field (declared on the Form-Bean class as an int that I call firstPageLoad) on the page that is used to control how the page is displayed. The value is initialized to 0 in the form-bean class, then upon submission in validate() it is set to 1. There is javascript in the web page above the jsp/html code that checks the value of firstPageLoad. If it is 0, then this is the first time the page is loading and nothing is done. If it is 1, then this is NOT the first time the page is loading and I check the values of the form variables that indicate whether or not radio buttons were selected or not and enable/disable text boxes as needed/appropriate.

    Paul
  3. Ok, thanx, I found a perfect solution with you're help!!

    But just for curiousity, I still have some little things to ask on your method, because i dont know very much about javascript.

    1)You said that you write all your Javascript above your jsp/html. But with me it only works when I place all my javascript under my html/jsp, because else he doesnt find my hidden formelement "firstPageLoaded". How did you do that?

    2)I also needed to make another hidden element : "hiddenRadio"
    In this element I need to store which radiobutton is selected, so I can acces it from my javascriptcode. Is it possible to find out in your javascript directly wchich radiobutton is selected? I mean, without hiddenradio Element. How do you do that?

    Thanx for the help!

    Olivier