roymustang86
roymustang86

Reputation: 8563

jsp pass variables between two pages

In my main jsp page, I have something like

<%@ include file="../customParametersHorizontal.jsp" %>

<tr>
<td align="center" class="ReportParamsGenerateButtonCell">
<html:submit onclick="if(preGenerateCriteria()){return true;} else {return false;}">Generate</html:submit>
</td>
</tr>

Within customParametersHorizontal.jsp, there is another include for dateSelection1.jsp, which validates date and time set on the form.

 <script>
function validateHHMM(inputField, message) {
    var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField);

    if (isValid) {

    }else {
        alert("Time must be entered in the format HH:MM AM/PM");

    }

    return isValid;
}
</script>

So, within dateSelection1.jsp, I tried to disable the button in the main jsp using :

 document.form1.ReportParamsGenerateButtonCell.disabled= true;

But, I get an error saying object is null or not defined.

Is there anyway, I can set a var, so that it can be chacked by the main form and disable the button accordingly?

Upvotes: 0

Views: 1323

Answers (2)

David Gorsline
David Gorsline

Reputation: 5018

Here's a way to do it, retaining the <html:submit> tag, and without introducing jQuery: First, add the styleId attribute so that your button has an HTML ID you can grab onto:

<html:submit styleId="my_submit" onclick="if(preGenerateCriteria()){return true;} else {return false;}">Generate</html:submit>

Next, use the ID to get the element of the DOM and disable it:

var elt = document.getElementById("my_submit");
elt.disabled = true;

Update

If I understand the original question correctly, main.jsp includes customParametersHorizontal.jsp, whici includes dateSelection1.jsp. So the three scripts are rendering just one HTML page, hence one document object.

I only see (partial) code for one <form> on the HTML page. Even if you do have more than one form, .getElementById() works across all DOM elements in all forms, as opposed to the document.form1 style of accessing elements.

Be sure that you call validateHHMM() at the appropriate point to enable/disable your submit button, and make sure that the button is re-enabled once the user corrects his input to make it valid.

Upvotes: 1

Flavio Cysne
Flavio Cysne

Reputation: 1456

document.form1.ReportParamsGenerateButtonCell will return null or error unless you have the elements with name attribute set to form1 and ReportParamsGenerateButtonCell like the example bellow:

<form name="form1">
    <input type="submit" name="ReportParamsGenerateButton" value="Generate" />
</form>

This way you could do document.form1.ReportParamsGenerateButton.disabled = true without problem.

Upvotes: 1

Related Questions