Reputation: 964
I am using jquery for doing this example. I have my checkbox declare
<input type="checkbox" checked="checked" id="checkbox1" />
<script>
$('#checkbox1').change(function() {
if (!$(this).is(':checked')) {
<% call the jave method %>
}
else
{
<%call the java method%>
}
</script>
The problem consist that when the page is loading the two actions are evaluated. I was thinking in using ajax but haven't found one example for check box change events.
Upvotes: 1
Views: 1568
Reputation: 11742
JavaScript code runs on the client (web browser), while java code runs on the server (servlet). They run on different machines. This is what your confusion is all about: they don't run next to each other. You should wore java in this context as a means of getting HTTP request, processing something on server, and sending back HTTP response. All in all, java code is to be executed during what's referred to as 'server-side processing' above.
Effectively, what you want to do is to fire an AJAX request on some JS event, do whatever you want in servlet and do some action with the returned data in a callback function.
You need to do some research to find out how to do that. As you seem to be using jQuery, $.ajax
is a good starting place.
Upvotes: 0
Reputation: 80
Hummm ... your code seems good, but to avoid the bord effect at page load, what do you think about using the $('#checkbox1').click()
event instead of change ?
The page load will not fire click event, like this you're already sure that the action come from your user.
Don't forget to check the checked attribute
like you already do and i think you're right.
Upvotes: 1