简体   繁体   中英

Passing javascript variable defined inside function to jsp without reloading

I want to load a bootstrap modal. The contents of that modal are dynamic based on onclick function on button.For this ,onclick of button, javascript function with one parameter is called. Function sets $('#modalId').show(); modal should contain content fetched from database based on parameter passed to javascript funtion. So for this I want to transfer the passed parameter from js to jsp of modal body. How to access that parameter which was passed on click.

The jsp code is evaluated on server side before resulting html code is sent to the browser, while js code is evaluated on client side in the browser. So there is no way to pass parameters to jsp code directly.

But of course there is a way to solve your problem. You need to make an ajax request in your javascript code when user clicks on that button. You can pass the parameter in your ajax request which might simply load another jsp which can access the request parameters, perform any database queries you like and return some html.

On client side (in javascript code) you simply need to place the returned html code in your modal dialog or wherever you like and display the results.

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