简体   繁体   English

如何将scriptlet变量传递给onclick()函数调用javascript

[英]How to pass a scriptlet variable into onclick() function call to javascript

I need to read a variable and then have to pass that as an argument to a function after onclick(). 我需要读取一个变量,然后在onclick()之后将其作为参数传递给函数。 I am using JSP, so I am reading the variable using scriptlets 我正在使用JSP,所以我使用scriptlet读取变量

<% int lateRegDays = 6;%>

I am passing the variable as 我传递变量为

onclick="displayDatePicker('startTestAdminNo','mdy',<%=lateRegdays %>,120,0,3);"

But, I am unable to pass the valueof 6. The string "<%=lateRegDays%>" is being passed to the function. 但是,我无法传递值6.字符串“<%= lateRegDays%>”正在传递给函数。 However, when I tried to print the value using alert, it worked. 但是,当我尝试使用alert打印值时,它可以工作。 The changes I did is, 我做的改变是,

onclick='<%="alert("+lateRegDays+")"%>'

In the similar fashion, I need to know, how to pass all my arguments to function with this scriptlet variable. 以类似的方式,我需要知道,如何通过这个scriptlet变量传递我的所有参数。 The entire code snippet is: 整个代码段是:

<td><input name="startTestAdminNo"></td>
  <td>
        <chtml:img srcKey="order.calendar.search" onclick="displayDatePicker('startTestAdminNo','mdy',<%=lateRegdays %>,120,0,3);"
            altKey="order.calendar.alt_search" bundle="prompt" />
  </td>

I need help with this as I am stuckand I don't know how to pass multiple arguments along with a scriptlet variable 我需要帮助,因为我被困,我不知道如何传递多个参数和scriptlet变量

Scriplets will generate the HTML content when the Browser loads the JSP page, so if you want to use it inside your javascript, you need to capture that lateRegDays variable during the page loading time as below: 当浏览器加载JSP页面时, Scriplets将生成HTML内容,因此如果要在javascript中使用它,则需要在页面加载时捕获该lateRegDays变量,如下所示:

<script>
var lateRegDays; <!-- global variable -->
function init() {
lateRegDays = '<%= lateRegDays %>';
}
function  displayDatePicker(...) {
  // Get the lateRegDays var
}
</script>
<body onload="init()">
<!-- your code -->
</body>

PS: Scriplets are legacy code (invented in early 2000 by Sun), which is not a best practice to use them, so avoid them by using the latest front-end technology stack for the presentation layer. PS: Scriplets是遗留代码(由Sun于2000年初发明),这不是使用它们的最佳实践,因此通过为表示层使用最新的前端技术堆栈来避免它们。

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

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