javascript 将jstl值传递给javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18014639/
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-27 10:27:31  来源:igfitidea点击:

passing jstl value to javascript

javascriptjspjstl

提问by Srinivas

Hi I've the following code in jsp. I am trying to pass the value to javascript as follows. But it is not working.The code is as follows:

嗨,我在jsp中有以下代码。我正在尝试将值传递给 javascript,如下所示。但是不行,代码如下:

    <td><button name = "btn" onclick="callMe(<c:out value='${bus.vehicleId}'/>")>Show Details</button></td>

(Vehicle id is printing if I use in my jsp.) Javascript is like this.

(如果我在我的 jsp 中使用,车辆 id 正在打印。)Javascript 是这样的。

    function callMe(myId) {
        alert(myId);}

Here I am getting value as "undefined". I've tried in different way as follows

在这里,我获得了“未定义”的价值。我以不同的方式尝试如下

    <td><button name = "btn" onclick="callMe('${bus.vehicleId}')>Show tails</button></td>

This is also not working.Please help me asap.

这也不起作用。请尽快帮助我。

回答by Pokuri

Your vehicleID might be string. If so, Yes! you get always undefined as a result. What ever the values coming as a result of <c:out value='${bus.vehicleId}'/>will be treated as a variable in html and javascript trying to print that variable value, which is undefined.

您的车辆 ID 可能是字符串。如果是这样,是的!结果总是未定义。作为结果的任何值都<c:out value='${bus.vehicleId}'/>将被视为 html 和 javascript 中的变量,试图打印该变量值,即undefined.

Try this way to let java script treat that jstl value as string literal

尝试这种方式让java脚本将该jstl值视为字符串文字

onclick="callMe('${bus.vehicleId}')">

Find single colon and double colons positions!

找到单冒号和双冒号的位置!

回答by user1597490

<tr class="txtnbmed">
<td align="center"><c:out value="${conPartRef.opCode}" /></td>
<td align="center"><html:select property="approvalStatus" indexed="true" name="conPartRef">
<option value="" <c:if test="${conPartRef.approvalStatus==''}">selected</c:if>>--- SELECT ---
<option value="A" <c:if test="${conPartRef.approvalStatus=='A'}">selected</c:if> >Approved
<option value="R" <c:if test="${conPartRef.approvalStatus=='R'}">selected</c:if> >Rejected
</option>
</html:select>
</td>
</c:forEach>

<input type="button" class="txtMF" value="Confirm & Print Supplement RO" onclick="doMarkAsOFP(this.form)">

function doMarkAsOFP(form) {
// need to get approvalStatus values and the opCode values.
submitPage();
}