using javascript variable inside jstl

前端 未结 3 1483
难免孤独
难免孤独 2020-12-16 03:17

I want to iterate a HashMap in javascript using jstl. is it possible to do like this?

function checkSelection(group,tvalue){
alert(group);
alert(tvalue);

&l         


        
3条回答
  •  囚心锁ツ
    2020-12-16 03:49

    It is not possible because JSP is executed first at the server side, then the JavaScript gets executed at the client side.

    You can still use the c:forEach to loop through the ${configuredGroupMap}, but you cannot do the comparison across groupMap.key and group directly.

    Instead, a solution in this case is to assign the server-side groupMap.key to a client-side variable in javascript first. Then use javascript for the if check, instead of c:if.

    I've modified your example to below

    function checkSelection(group,tvalue){ 
    alert(group); 
    alert(tvalue); 
    
     
        alert(""); 
        var groupKey = "";
    
        if (groupKey == group){
            alert(""); 
    
        var groupValue = "";
            if (groupValue == tvalue){
                alert("both are equal"); 
        }
        }
     
    } 
    

提交回复
热议问题