将Scriptlets中的变量传递给Javascript。 [英] Passing a variable from Scriptlets to Javascript.

查看:138
本文介绍了将Scriptlets中的变量传递给Javascript。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码片段::

I have this code snippet ::

<script type="text/javascript">
function gotoa(){
    <%!
    public void a(){
     String temp1;
    PopulateTextbox obj = new PopulateTextbox();
    temp1 = obj.method();
    request.setAttribute("variable", temp1);
    }
    %>


var myVar = <%=request.getAttribute("variable")%>
}
</script>

我想做的是在JavaScript函数gotoa()中获取变量temp1的值。在这个特定的代码中,我收到错误的无效请求。

What i want to do is to get the value of variable temp1 in my JavaScript function gotoa(). In this particular code i am getting an error invalid request

request.setAttribute("variable", temp1);

我的主要目的是在一些按钮点击事件上调用函数a(),以便我的脚本让代码再次运行,并在变量temp1中传递新的值。然后传递给gotoa()作为我的数据网格的源(不在本代码中)。基本上我想刷新网格上一些按钮点击。我在做正确的做法吗请帮忙。谢谢。

My main aim is to call the function a() on some button click event so that my script let code runs again and fresh values are being passed in variable temp1. which will then passed on to gotoa() to act as a source for my data grid(not in this code). basically i want to refresh grid on some button click. Am i doing the right way. Please help. Thanks.

推荐答案

当您需要 temp1 c> gotoa()执行以下操作:

When you need value of variable temp1 inside gotoa() do the following:

<%  String temp1; // Store value in temp1 variable for later use
    PopulateTextbox obj = new PopulateTextbox(); 
    temp1 = obj.method();
%>
<script>
function gotoa(){ 

    var temp1Val = document.getElementById("hiddenTemp1").value;
    // put your logic here
    document.getElementById("hiddenTemp1").value = tempVal1;
}
</script>
<body>
<form action="otherPage.jsp">
    <!-- use the value of temp1 variable -->
    <input type="hidden" name="hiddenTemp1" id="hiddenTemp1" value="<%=temp1%>">
    <input type="button" onclick="gotoa()" value="GotoA">
    <input type="submit" value="Submit New Value">
</form>
</body>

首先,将值分配给变量temp1。然后,使用scriptlet将带有value = temp1的隐藏输入组件呈现给JSP。如果要验证,只需查看生成的HTML的源代码,您应该看到输入隐藏的值等于该变量。

First you assign the value to variable temp1. And then you Render your JSP with a Hidden Input component with value=temp1 by using scriptlet. If you want to verify, just View the Source of generated HTML and you should see the value of input hidden equal to the variable.

当表单提交时, hiddenTemp1将在请求中可用。
如果您打算更改此隐藏组件的值,可以将该值设置回组件中。

When the form is submitted the value of hiddenTemp1 will be available in Request. If you intend to change the value of this hidden component, you can set the value back in the component.

这篇关于将Scriptlets中的变量传递给Javascript。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆