本文介绍了在JSP代码中使用javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSP代码中使用javascript变量.我知道存在客户端和服务器端变量的问题,但是仍然可以在不与服务器或Servlet进行交互的情况下实现此目的?

I want to use javascript variables in JSP code . I know there is issueof Client side and server side variables , but still any way to do so without interacting with server or servlet??

代码:

<form>
    <select name="select" id="form_events">
        <%
            for (int i = 1; i <= 5; i = i + 1) {
        %>
        <option value=<%=i%>>
            <%=i%></option>
        <%
            }
        %>

    </select>

</form>

任何人都可以帮助我为变量"val"赋值吗?

Can any one help me in assigning value to variable "val" ??

推荐答案

您不能,JSP在服务器端,JS在客户端大小.另外,您不应该使用scriptlet,它会使您的代码类似于一碗意大利面.

You can't, JSP is on the server side, JS is on the client size. Also, you should not use scriptlets, it makes your code similar to a bowl of spaghetti.

如果您只需要生成JavaScript代码,则可以执行此操作,例如通过使用以下语法:

If you only need to generate JavaScript code you can do that e.g. by using this syntax:

var variable = '<%=myJspVariable.toString()%>';

这会将myJspVariable呈现为字符串,并且可以在您的JavaScript代码中使用.

This renders the myJspVariable as a string, and it can be used in your JavaScript code.

这篇关于在JSP代码中使用javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:25
查看更多