简体   繁体   English

如何在JavaScript内联属性代码中引用/编写JSP变量?

[英]How to quote/write JSP variable in JavaScript inline attribute code?

I need to send variables at runtime to a js method for ajax. 我需要在运行时将变量发送到ajax的js方法。 I am trying with this onclick function but it gives syntax error on click.: 我正在尝试使用此onclick函数,但单击时给出语法错误。

<c:set var="abc" value="${myTaglib:getAbc()}" />
<a href="#" onclick="sendAjax(${abc.firstVal}, ${abc.secVal}, ${abc.thirdVal}, ${abc.fourthVal}, ${abc.fifthVal})"> Click Here !!!</a>

Some of these values may have spaces and decimals in them. 其中一些值中可能包含空格和小数。 On click, I get this error at the first occurrence of space.: 单击时,在第一次出现空格时出现此错误。:

SyntaxError: missing ) after argument list

The actual HTML looks like: 实际的HTML看起来像:

<a onclick="sendAjax(240265, Workplace Ethics Exam, Company Security Policy, Friends Life, ABF/45RFG, 41444.1830)" href="#">Click Here !</a>

What is wrong with this JS call ? 这个JS调用有什么问题?

Spaces in your JavaScript code are most likely a syntax error. JavaScript代码中的空格很可能是语法错误。 If your values have spaces, it's best to treat them as strings. 如果您的值包含空格,则最好将它们视为字符串。

onclick="sendAjax('${abc.firstVal}', '${abc.secVal}', ... '${abc.fifthVal}')">

If you want to use them as numbers later, you can use the JavaScript functions parseInt and parseFloat to turn strings into numbers. 如果以后想将它们用作数字,则可以使用JavaScript函数parseIntparseFloat将字符串转换为数字。

您是否尝试过在最后一个花括号后留一个空格?

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

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