Scriptlet in js

蓝咒 提交于 2019-12-02 15:01:17

问题


i have a jsp page... i am adding some content to page dynamically depending upon request parameters (an array will be returned by request) based on this i have to create a drop down. i want to do this on change of another drop down.. so can be done using javascript only but i am unable to use scriptlet in js, is this really possible??

EDIT : i wanna perform some actions on the values retrieved from scriptlet as well

it will be of this sort

function changeMethod(){
    var templateselected = document.getElementById("templateDropDown");
    var versionDropDown = document.getElementById("versionDropDown");
    if ( templateselected.options.selectedIndex != -1)
    {
        var selected=templateselected[templateselected.options.selectedIndex].value;
        removeChildNodes(versionDropDown);
        <% 
        RetrieveTempSecVersions[] lsListOfFiles = (RetrieveTempSecVersions []) request.getAttribute("templateNames") ;
        for (int i=0 ; i < lsListOfFiles[1].getVersionNumber().length ; i++ ) {
            System.out.println("helllooooo");%>
        versionDropDown.innerHTML+='<OPTION VALUE="'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'">'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'</OPTION>';
        <%}%>
    }
}

回答1:


Yes you can have something like this

function addCombo() {
    var textb = document.getElementById("txtCombo");
    var combo = document.getElementById("combo");

    var option = document.createElement("option");
    <c:forEach var="state" items="${stateList}" varStatus="status">  
    option.text = "${state}";
    option.value = "${state}";
    try {
        combo.add(option, null); //Standard
    }catch(error) {
        combo.add(option); // IE only
    }
    </c:forEach>
    textb.value = "";
} 
  • Also See

Note: I haven't tested this code , this is just a demonstration




回答2:


If the javascript is inline or declared in the same jsp page, there is no problem. Something like:

<script type="text/javascript">
var foo = '${foo}'; // or <%= foo => if you like
</script>

If it is in a separate .js file, then you should serve the .js file through a special servlet.



来源:https://stackoverflow.com/questions/4509312/scriptlet-in-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!