如何将对象作为参数传递给onClick上的Java脚本函数 [英] How to pass Object as parameter to java script function on onClick

查看:303
本文介绍了如何将对象作为参数传递给onClick上的Java脚本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目标: 当我单击链接时,应打开一个新窗口,并在该窗口中显示整个日志文件的内容,并且该窗口不应具有地址栏和导航按钮(后退,前进).

My main goal: When i click the link, a new window should be opened and display the content of entire log file in that window and the window should not have an address bar and navigation buttons (Back, Forward).

还有其他方法可以实现我的目标吗?

Is there any other approach that i can reach my goal?

这是我想做的事情,我试图通过传递日志文件的url来调用java脚本方法,并考虑实现代码以打开新窗口

Here is what i am trying to do, I am trying to call the java script method by passing the url of log file and thought of implementing the code to open new window

<table id="serverLogsStats" class="serverLogsView">
   <c:forEach var="log" items="${logsList}" varStatus="i" >
      <tr>
        <td>${log.name}</td>            
        <td><a href="#" onclick="openLog('${log}');">Open log</a></td>        
      </tr>
   </c:forEach>
</table>


<script>
function openLog (log) {
    var abc = log.name;
    alert(abc);    
}
</script>

当我将对象'log'传递给openLog函数时,该值作为字符串传递.我希望日志作为对象传递.

When i pass an object 'log' to the openLog function, the value is passing as a String. I want the log to be passed as an object.

这里我的输出是未定义的.

Here my output is coming as undefined.

如果我输入"alert(log);"它正在打印对象的地址

If i put 'alert(log);' it is printing the address of the object

推荐答案

这应该有效:

<table id="serverLogsStats" class="serverLogsView">
   <c:forEach var="log" items="${logsList}" varStatus="i" >
      <tr>
        <td>${log.name}</td>            
        <td><a href="#" onclick="openLog('${log.name}');">Open log</a></td>        
      </tr>
   </c:forEach>
</table>


<script>
function openLog(logName) {
    alert(logName);    
}
</script>

您不能使用Java对象并将它们直接用作JS对象,只能传递基元.

You can't use java objects and use them directly as JS object, you can only pass primitives.

对于您的用例,您可以让URL服务器端以日志名称作为参数并显示其内容.

For your use case, you could have an URL server side taking a log name as parameter and displaying it's content.

这篇关于如何将对象作为参数传递给onClick上的Java脚本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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