NIVESH SENGAR
NIVESH SENGAR

Reputation: 1333

Radio button disabled first, after submit again enabled

I have a jsp where some radio buttons and textboxes depend on dropdown selection.
Here is my jsp and javascript code.

jsp

         <s:form action="crInquiry" name="form">    
            <s:select name="filterValue" list="headerList"
    onchange="OnChange(this.form.filterValue);" />
            <s:radio name="filter" list="#{'STATUS_FILTER_START':'START','STATUS_FILTER_END':'STOP'}" 
label="Stage"></s:radio>
            <s:textfield disabled="true" value="0" name="count" theme="css_xhtml"></s:textfield>
            <s:radio name="ascOrder"  list="#{'ASC':'ASC','DESC':'DESC'}"></s:radio>
            <s:submit value="Filter" onclick="gotopage('FilteredInquiryLog')"></s:submit>
            <s:submitvalue="Details" onclick="gotopage('crInquiry')"></s:submit>
            </s:form>

javascript

<script type="text/javascript">
    function OnChange(dropdown) {
        var myindex = dropdown.selectedIndex;
        document.form.filter[0].disabled = false;
        document.form.filter[1].disabled = false;
    if (myindex == 8) {
            alert("8");
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = false;
            document.form.submit.disabled = false;
        }
        else if (myindex == 9) {
            alert("9");
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
        }

        else{
            document.form.filter[0].disabled = false;
            document.form.filter[1].disabled = false;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
            }
        }


    function gotopage(actionname)
    {   
            document.form.action=actionname+".action";
            document.form.submit();
    }
</script>

The problem is when I select an item on dropdown say element no. 8 or 9 so by javascript radio buttons should be disabled for both and textfield and radio button for 9. When I select an item it disabled dependent radion button or textfield perfectly but when I submit, it show me the radio button enabled because I come to the same jsp. What is the problem in my javascript?

Upvotes: 2

Views: 2194

Answers (2)

Naved
Naved

Reputation: 4128

If I understood your problem correctly, I think there is no problem with your Javascript.
Because there is no communication between the page data before submit and after submit it will create problem.

You can overcome with this problem by one of the following ways. 1. Call your javascript on "OnLoad" event of body and it will work same to you.
2. Store the current status of the page in some control (say text fields) and submit it with other data of page. At the time of loading (may be onLoad or at the end of your JSP page) you can restore the state using the value in that field.

I hope this would work for you.

Upvotes: 0

Avi Y
Avi Y

Reputation: 2495

You should check your dropdown value also on page onload event.

Something like this

<script type="text/javascript">

/**
* This function will be called twice - once on onChage event, and once on onLoad event
*/
    function OnChange() {
    dropdown = document.getElementById('myDropDown');
        var myindex = dropdown.selectedIndex;
        document.form.filter[0].disabled = false;
        document.form.filter[1].disabled = false;
    if (myindex == 8) {
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = false;
            document.form.submit.disabled = false;
        }
        else if (myindex == 9) {
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
        }

        else{
            document.form.filter[0].disabled = false;
            document.form.filter[1].disabled = false;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
            }
        }


    function gotopage(actionname)
    {   
            document.form.action=actionname+".action";
            document.form.submit();
    }
</script>

<body onload="OnChange()">
<s:form action="crInquiry" name="form">    
            <s:select name="filterValue" list="headerList" id="myDropDown"
    onchange="OnChange();" />
            <s:radio name="filter" list="#{'STATUS_FILTER_START':'START','STATUS_FILTER_END':'STOP'}" 
label="Stage"></s:radio>
            <s:textfield disabled="true" value="0" name="count" theme="css_xhtml"></s:textfield>
            <s:radio name="ascOrder"  list="#{'ASC':'ASC','DESC':'DESC'}"></s:radio>
            <s:submit value="Filter" onclick="gotopage('FilteredInquiryLog')"></s:submit>
            <s:submitvalue="Details" onclick="gotopage('crInquiry')"></s:submit>
            </s:form>

Upvotes: 1

Related Questions