简体   繁体   中英

Fire jquery event from Java

I have few elements generated dynamically with a jQuery click event on each element defined as follows:

$(".addToCart").live("click", function(e) {
 <!-- function body goes here -->
});

Now I need to implement button that adds all elements like if used clicked on each one of them. I have implemented all logic in a servlet but now after I click the "Add all" button I need to refresh the page to get the UI refreshed. I wonder if I can fire a jQuery event from servlet in order to update GUI.

You will need to update the UI within the Javascript. Java and JQuery are to separate beasts and you cannot call functions between them. Eg Java cannot call javascript, javascript cannot call Java.

Instead you should add a JQuery function to "add all" similar to the one you have shown:

$(".addAll").live("click", function(e) {

    <!-- Send AJAX request to tell the server that the user has added all items to the cart -->L
    <!-- If there are no errors, update UI to reflect the add all button being pressed -->
});

If you need the function to update something server side, then that function will need to make an AJAX call to tell the server what has been added to the cart. See http://api.jquery.com/jQuery.ajax/

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