简体   繁体   中英

Extract JSTL value from a list in to javascript variable

I have an issue where I am doing like this:

<Script>
var counts=1;
travelExpense.value='<c:out value="${customerPO.roleList["'+counts+'"].travelExpense}"/>';
</Script>

but I am unable to concatenate "counts" js variable with JSTLtag.It says Unterminated <c:out I am unable to write the correct syntax for the same

You are missing the second concatenation character:

<Script>
var counts=1;
travelExpense.value='<c:out value="${customerPO.roleList["'+counts+'"].travelExpense}"/>';
//                                                            HERE^
</Script>

Update given the new information. Try escaping the quotes that go around count :

<Script>
var counts=1;
travelExpense.value='<c:out value="${customerPO.roleList[\'' + counts + '\'].travelExpense}"/>';
</Script>

This yields the following string:

<c:out value="${customerPO.roleList['1'].travelExpense}"/>

As an aside, are you sure the 1 should be quoted? If roleList is an array then this should work:

travelExpense.value='<c:out value="${customerPO.roleList[' + counts + '].travelExpense}"/>';

Giving:

<c:out value="${customerPO.roleList[1].travelExpense}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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