[英]Pass a value from <% %> to <script> (JSP)
I have the following code, I would like to know if there is a way I can use the variable "prov" in the tag part 我有以下代码,我想知道是否有一种方法可以在标签部分中使用变量“ prov”
<%
String prov;
if(request.getParameter("btnBusProv")!= null)
{
prov = request.getParameter("cbProv");
out.println("Nombre del proveedor: ");
out.println(prov);
}
%>
<script>
function MostrarNombres()
{
document.getElementById("txtRUC").value = prov;
document.getElementById("txtFec").value = prov;
document.getElementById("txtDIR").value = prov;
}
</script>
You can use hidden variables for this purpose, for example: 您可以为此使用隐藏变量,例如:
<input type="hidden" id="prov" value='<%=request.getParameter("cbProv") %>' >
and then, in the script block: 然后,在脚本块中:
var obj = document.getElementById("prov");
There's no need to use a hidden input element when all you need is the value of a request parameter. 当您需要的只是请求参数的值时,无需使用隐藏的输入元素。 Use
${param.cbProv}
to access that value without scriptlets. 使用
${param.cbProv}
无需脚本即可访问该值。
<script>
var prov;
<c:if test="${not empty param.btnBusProv}">
prov = '${param.cbProv}';
</c:if>
function MostrarNombres()
{
document.getElementById("txtRUC").value = prov;
document.getElementById("txtFec").value = prov;
document.getElementById("txtDIR").value = prov;
}
</script>
You can use JSP Expressions: 您可以使用JSP表达式:
<script>
function MostrarNombres()
{
document.getElementById("txtRUC").value = <%=prov%>;
document.getElementById("txtFec").value = <%=prov%>;
document.getElementById("txtDIR").value = <%=prov%>;
}
</script>
Or a more straightforward way would be to set it directly in a hidden html dom element and use it: 或更直接的方法是直接在隐藏的html dom元素中设置它并使用它:
<input id='prov' type="hidden" value='<%=request.getParameter("cbProv")%>'>
<script>
function MostrarNombres()
{
var prov = document.getElementById('prov').value;
document.getElementById("txtRUC").value = prov;
document.getElementById("txtFec").value = prov;
document.getElementById("txtDIR").value = prov;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.