简体   繁体   English

我们可以将jsp变量传递给jQuery ready函数吗

[英]Can we pass a jsp variable to jQuery ready function

I a have a jQuery ready function given below, I need to pass a value to the myLocFn() . 我有一个下面给出的jQuery ready函数,我需要将一个值传递给myLocFn() What I am asking is, as we are using ready function ,if it possible to pass a jsp value to myLocFn(). 我要问的是,当我们使用ready函数时,是否有可能将jsp值传递给myLocFn()。

<script>  
$(document).ready(function(){
    $(".myDiv").myLocFn();     // need to pass a value here                  
});
</script>

I have included the js path as well <script src="myLocFn.js"></script> . 我已经包括了js路径以及<script src="myLocFn.js"></script> Where the my js starts with, 我的js开头的地方

(function($){
    $.fn.myLocFn= function(options) {
..... // need to get the value here

Write a JSP variable inside script section using server code: 使用服务器代码在脚本部分中编写一个JSP变量:

<script type="text/javascript">

var jspVar="JSP_value"; // use expression tag in JSP here
</script>

Then refer in your jQuery code, just like a JavaScript variable, as u assigned ur JSP value to JS variable in above line 然后像上JavaScript变量一样引用您的jQuery代码,因为您在上一行中将您的JSP值分配给了JS变量

(function($){
    $.fn.myLocFn= function(options) {
    alert(jspVar); //here u can see 
}

Edit: Please make sure that jspVar should be defined before accessing. 编辑:请确保在访问之前应定义jspVar。 You can check this from the rendered HTML 您可以从呈现的HTML中进行检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM