简体   繁体   中英

jquery .html(' <%jsp scriplet%>');

I would like a for loop in jquery using .html() like this:

.html('<select property="doctype" name="doctype"><%for (String number : list)
{%>'<option value="'<%=number%>'>'<%out.println(number); %>'</option>'<% } %>'</select>');

In Java's for each loop list it uses an object of java.util.ArrayList<String> .

Here the .html(); function will call when we click on add button. Here my question is it possible to write jsp scriplet code in .html() of jquery.

function will call when we click on add button.

No you can't.

Jsp is compile time.

More over java script plays on client side and jsp playes on server side.

jsp ,jsf and other kinds of java web technologies are rendered on the server side. Since jquery is a client side technology, it's not possible.

Instead, you can make ajax calls via jquery and update the html.

You cannot have the client execute Java in your scriptlet. Fortunately, what you want to do is very common.

Don't try to dynamically generate JavaScript in a scriptlet or jsp. It's very easy to make a mistake and end up with malformed JavaScript.

Instead, use a .jsp to spit out HTML. Then use static JavaScript to grab that HTML and put it where you want in the DOM.

For example, your jsp file could look something like this:

<div id="destination">The select element will be added to this div.</div>

<select id="my-select" property="doctype" name="doctype"> 
<c:forEach  items="${list}" var="number">
   <option value="${number}">${number}</option>
</c:forEach>
</select>

<script>
    $('#destination').append($('#my-select'));
</script>

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