简体   繁体   English

jsp在两个页面之间传递变量

[英]jsp pass variables between two pages

In my main jsp page, I have something like 在我的主要jsp页面中,我有类似的东西

<%@ 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. 在customParametersHorizo​​ntal.jsp中,还有另一个包含dateSelection1.jsp的包含,它验证表单上设置的日期和时间。

 <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 : 所以,在dateSelection1.jsp中,我尝试使用以下命令禁用主jsp中的按钮:

 document.form1.ReportParamsGenerateButtonCell.disabled= true;

But, I get an error saying object is null or not defined. 但是,我得到一个错误,说对象为null或未定义。

Is there anyway, I can set a var, so that it can be chacked by the main form and disable the button accordingly? 无论如何,我可以设置一个var,以便它可以被主窗体追逐并相应地禁用按钮?

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: document.form1.ReportParamsGenerateButtonCell将返回null或error,除非你有name属性设置为form1的元素和ReportParamsGenerateButtonCell如下例所示:

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

This way you could do document.form1.ReportParamsGenerateButton.disabled = true without problem. 这样你就可以没有问题地执行document.form1.ReportParamsGenerateButton.disabled = true

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>标签,而不引入jQuery:首先,添加styleId属性,以便您的按钮具有可以抓取的HTML ID:

<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: 接下来,使用ID获取DOM的元素并禁用它:

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. 如果我正确理解原始问题,main.jsp包含customParametersHorizo​​ntal.jsp,whici包含dateSelection1.jsp。 So the three scripts are rendering just one HTML page, hence one document object. 因此,这三个脚本只渲染一个HTML页面,因此只有一个文档对象。

I only see (partial) code for one <form> on the HTML page. 我只在HTML页面上看到一个<form> (部分)代码。 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. 即使您有多个表单,.getElementById()也适用于所有表单中的所有DOM元素,而不是访问元素的document.form1样式。

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. 确保在适当的位置调用validateHHMM()以启用/禁用提交按钮,并确保在用户更正其输入以使其有效后重新启用该按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM