struts2 属性值作为 javascript 函数的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12507817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:22:58  来源:igfitidea点击:

struts2 property value as argument of javascript function

javascriptstruts2

提问by Rohit R.K.

I have one in jsp page.

我在jsp页面中有一个。

i want to pass this value as argument of java script function call

我想将此值作为 java 脚本函数调用的参数传递

my jsp apge code is:

我的jsp apge代码是:

  <input type="submit" onclick="play(<s:property value="vname"/>)">

my java script function code:

我的java脚本函数代码:

  function play(n)
    {
        alert(n);

    }

please help me to solve this problem;

请帮我解决这个问题;

regard

看待

Rohit kachhadiya

罗希特·卡查迪亚

采纳答案by daveoncode

You have to wrap struts tags with quotes:

你必须用引号包裹 struts 标签:

<input type="submit" onclick="play('<s:property value="vname"/>')">

otherwise the value of "vname" will be interpreted as a JavaScript variable, not a string!

否则“vname”的值将被解释为一个 JavaScript 变量,而不是一个字符串!

回答by Accollativo

Using struts2 tag:

使用 struts2 标签:

<s:submit onclick="javascript:play('%{vname}')" />

回答by Ravi A

Agree with daveoncode, apart from that In your js, get it directly like

同意daveoncode,除此之外在你的js中,直接获取就好了

var varName = document.forms[0].name/id.value;

Or you have one more option to write inline js and pass value of your element. But its a bad practice.

或者,您还有另一种选择来编写内联 js 并传递元素的值。但这是一个不好的做法。