admin管理员组

文章数量:1336660

I have the following scenario

<select name="dropdown_Source" onchange="call()">
    <% for (int i=0 ; i < a1.length; i++) { if (a1[i]==n ull) break; %>
        <option value="<%=a1[i]%>">
            <%=a1[i]%>
        </option>
    <% } %>
</select>

<script>
    function call() {
        var source = document.forms[0].dropdown_source.value;
        // Now this 'source' value i have to pass to jsp function 
        <%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>
    }
</script>

How this source value should I pass in JSP function? a.getResult() - function will return me list of dropdown elements and that list I have to populate in another <option> tag.

I have the following scenario

<select name="dropdown_Source" onchange="call()">
    <% for (int i=0 ; i < a1.length; i++) { if (a1[i]==n ull) break; %>
        <option value="<%=a1[i]%>">
            <%=a1[i]%>
        </option>
    <% } %>
</select>

<script>
    function call() {
        var source = document.forms[0].dropdown_source.value;
        // Now this 'source' value i have to pass to jsp function 
        <%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>
    }
</script>

How this source value should I pass in JSP function? a.getResult() - function will return me list of dropdown elements and that list I have to populate in another <option> tag.

Share Improve this question edited May 11, 2013 at 12:40 Vitalii Petrychuk 14.3k8 gold badges54 silver badges55 bronze badges asked May 11, 2013 at 12:27 Rachana KRachana K 1041 silver badge4 bronze badges 1
  • ugly code.. I dont know jsp but I am sure this will not work because jsp server-side javascript client-side.. If you can get dropdown value with jsp do it.. With asp I can get value of dropdown which I set its value from javascript. – Mehmet Commented May 11, 2013 at 12:33
Add a ment  | 

3 Answers 3

Reset to default 3

You cannot do that. JavaScript executes on the client side after JSP page is already piled and sent to the browser as response.

You can not mix javascript and java code. when you do

<%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>

This means that code snippet will be processed at server side as this is plain java code.Now variable "source" is not java variable but a javascript variable which will e into picture when your html page loads on browser. So its a pilation error.

The best thing to do here is, once the javascript executes, take that source value and make an AJAX call to your server, process it and send the values back to the client and then populate your dropdown.

本文标签: Passing a variable value from javascript to JSPStack Overflow