javascript onclick 中带有 javascript 参数的 Jsp
我想插入对函数的调用来获取 JavaScript onclick
函数的参数。我想做的是这样的:
<input class="refreshbutton"
type="button"
id="searchUfficiPopup"
onClick="javascript:postColorbox('/DeliDete/searchUfficiPopupBySettoreId', **'&settoreIdKey=${javascript:getSettoriId()}'**, 'tabUfficiForm', 'initScriptUffici')"
value="<fmt:message key="navigation.searchUffici"/>" />
这样 Eclipse 就会告诉我函数 javascript:getSettoriId()
未定义。我在外部 .js 文件中定义了这个函数,在运行时使用 jQuery 的 .getScript
加载,所以我不想将它插入到 jsp 中(无论如何我尝试将它插入到 jsp 中,但是 IDE仍然说该函数未定义)。
函数 postColorbox 定义为:
function postColorbox(url, parameters, formName, initScript)
函数 getSettoriId()
返回先前输入的表单元素 Settori
的值,我需要执行受限查询(我需要获取与所选 Settori 实体相关的所有 Uffici 实体)
说到这里,我想请教各位专家:
是否可以使用 JavaScript 函数作为 onclick JavaScript 函数的参数?
如果我把要调用的这个函数放在外部 .js 文件中,jsp 能够看到它并调用它吗?
谢谢大家的帮助! 安德里亚
I would like to insert a call to a function to get a parameter for my JavaScript onclick
function. What I would like to do is something like:
<input class="refreshbutton"
type="button"
id="searchUfficiPopup"
onClick="javascript:postColorbox('/DeliDete/searchUfficiPopupBySettoreId', **'&settoreIdKey=${javascript:getSettoriId()}'**, 'tabUfficiForm', 'initScriptUffici')"
value="<fmt:message key="navigation.searchUffici"/>" />
This way Eclipse tells me the function javascript:getSettoriId()
is undefined. I defined this function in an external .js file, loaded at runtime with jQuery's .getScript
, so I would not like to insert it into the jsp (anyway I tried to insert it into the jsp but the IDE still says that the function is not defined).
The function postColorbox is defined as:
function postColorbox(url, parameters, formName, initScript)
The function getSettoriId()
returns the value of a previously entered form element, Settori
, which I need to perform a restricted query (I need to obtain all Uffici entities related to the selected Settori entity)
Told this, I would like to ask you experts:
Is it even possible to use a JavaScript function as a parameter of an onclick JavaScript function?
If I put this function to be called in an external .js file will the jsp be able to see it and call it?
Thank you all for your help!
Andrea
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除
上的
onClick
并使用 jQuery 事件处理程序来执行此操作:Remove the
onClick
on your<input>
and do it with a jQuery event handler instead:从 HTML 元素内部调用函数不是首选方法。如果可以的话,只需为元素分配一个 id 或类,然后在页面加载时使用 javascript 添加一个侦听器。
这样您就不会混合数据和操作。修改代码也会更容易,因为您的代码将位于 js 文件中。
Calling functions from inside the HTML element is not a preferred way. If you can - just assign the element an id or class, and then add a listener to it using javascript on page load.
This way you don't have your data and operations mixed. It will be also easier to modify the code, as your code will be located in js files.