开发者

jsp in javascript

开发者 https://www.devze.com 2023-02-04 20:15 出处:网络
I\'ve created a jsp page. In that when i select 1 check-box or both check-box or none, the corresponding text-boxes and list-box must be displayed in the same page.

I've created a jsp page. In that when i select 1 check-box or both check-box or none, the corresponding text-boxes and list-box must be displayed in the same page.

For that i tried of calling a javascipt function when i click the checkbox. The javascript function contain code to display the textboxes. But it didn't work.

Since I'm doing this project in struts, I don't know how to get check-box value. And calling of JavaScript function works. But didn't enter into jsp code in JavaScript function.

My code is

 <tr>
 <td>SEJ:</td>
 <td>SEJ 1:<html:checkbox property="sej1" value="on" onclick="checkbox_trial()"></html:checkbox></td>
 <td>SEJ 2:<html:checkbox property="sej2" value="on" onclick="checkbox_trial()"></html:checkbox></td>
 </tr>


  <script type="text/javascript">
  function checkbox_trial()
  {

        <% if(request.getParameter("sej1")=="on"){
    %> 
   <tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
   <tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
   <%} 

       else if(request.getParameter("sej2")=="on"){%>
   <tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
       <tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
   <%}

       else if((request.getParameter("sej1")=="on")&&(request.getParameter("sej2")=="on")){%>
   <tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
   <tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
   <tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
   <tr><td>SEJ2 DOI<html:text pro开发者_C百科perty="sej2_doi"></html:text></td></tr>
   <%}

       else{%>
   NOTHING <% } %>
  }


This is how it works: Java/JSP runs at webserver, produces HTML/CSS/JS, webserver sends it to webbrowser, webbrowser runs HTML/CSS/JS. Not Java/JSP. Rightclick the page in webbrowser and choose View Source. If Java/JSP has done its job right, you shouldn't see any line of it in there.

If you want to invoke Java/JSP code using JavaScript, you've got to invoke another HTTP request to the webserver so that it can execute the Java/JSP code based on the specific request. You can do that by either submitting the form or firing an asynchronous (ajaxical) request.

Given the skills shown as far and the fact that you're using Struts, I think ajax is going to be a bit too complex. I'd suggest to just submit the form on click of the checkbox

<input type="checkbox" name="show" value="true" onclick="submit()" />

and then let JSP conditionally display the input fields (just a JSTL example since I don't do Struts)

<c:if test="${param.show == 'true'}">
    <input type="text" />
    <select />
</c:if>

Update: you've by the way another major problem in the code. You can't compare string values by == in Java code (you can do so in EL only). In Java code you need to use equals() method. Otherwise they will be compared by reference instead of by value. I'd suggest to learn basic Java as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消