如何在JavaScript内联属性代码中引用/编写JSP变量? [英] How to quote/write JSP variable in JavaScript inline attribute code?

查看:114
本文介绍了如何在JavaScript内联属性代码中引用/编写JSP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时将变量发送到ajax的js方法.我正在尝试使用此onclick函数,但单击时会出现语法错误.:

I need to send variables at runtime to a js method for ajax. I am trying with this onclick function but it gives syntax error on click.:

<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

实际的HTML看起来像:

The actual HTML looks like:

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

此JS调用有什么问题?

What is wrong with this JS call ?

推荐答案

JavaScript代码中的空格很可能是语法错误.如果您的值包含空格,则最好将它们视为字符串.

Spaces in your JavaScript code are most likely a syntax error. If your values have spaces, it's best to treat them as strings.

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

如果以后想将它们用作数字,则可以使用JavaScript函数parseIntparseFloat将字符串转换为数字.

If you want to use them as numbers later, you can use the JavaScript functions parseInt and parseFloat to turn strings into numbers.

这篇关于如何在JavaScript内联属性代码中引用/编写JSP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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