如何将值从 JSP 页面传递到 JavaScript 页面?

huangapple go评论84阅读模式
英文:

How can I pass a value from a jsp page to a javascript page?

问题

我在我的jsp文件中有这段代码:

     int hours=5;
     int minutes=3;
     int seconds=2;

<h3 id="c_down">剩余时间 <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script src="countdown.js"></script>

这是我的JavaScript文件(countdown.js):

let minutes=
let seconds = 
let hours= 

let timming_onSeconds=seconds+minutes*60+hours*3600;

const cd=document.getElementById('c_down');

setInterval(update_countdown, 1000);

function update_countdown() {
    hours=Math.floor(timming_onSeconds/3600);
    minutes=Math.floor((timming_onSeconds%3600)/60);
    seconds=(timming_onSeconds%3600)%60;

    seconds=seconds <= 9 ? "0"+seconds : seconds;
    minutes=minutes <= 9 ? "0"+minutes : minutes;
    hours=hours <= 9 ? "0"+hours : hours;

    cd.innerHTML = `剩余时间 <br> ${hours} : ${minutes} : ${seconds}`;

    timming_onSeconds--;
}

如何将jsp文件中的seconds、minutes和hours的值传递给JavaScript文件中的seconds、minutes和hours呢?

英文:

I have this code in my jsp file:

     int hours=5;
     int minutes=3;
     int seconds=2;

&lt;h3 id=&quot;c_down&quot;&gt;TIME LEFT &lt;br&gt; &lt;%=hours%&gt; : &lt;%=minutes%&gt; : &lt;%=seconds%&gt; &lt;/h3&gt;
&lt;script src=&quot;countdown.js&quot;&gt;&lt;/script&gt;


This is my javascript file (countdown.js) :



let minutes=
let seconds = 
let hours= 


let timming_onSeconds=seconds+minutes*60+hours*3600;

const cd=document.getElementById(&#39;c_down&#39;);


setInterval(uptade_countdown, 1000);

function uptade_countdown() {


    hours=Math.floor(timming_onSeconds/3600);
    minutes=Math.floor((timming_onSeconds%3600)/60);
    seconds=(timming_onSeconds%3600)%60;


    seconds=seconds &lt;= 9 ? &quot;0&quot;+seconds : seconds;
    minutes=minutes &lt;= 9 ? &quot;0&quot;+minutes : minutes;
    hours=hours &lt;= 9 ? &quot;0&quot;+hours : hours;


    cd.innerHTML = `TIME LEFT &lt;br&gt; ${hours} : ${minutes} : ${seconds}`;


   timming_onSeconds--;


}

How can I give the values from seconds, minutes, hours that are from jsp file to seconds, minutes, hours that are in my javascript file?

答案1

得分: 1

你可以使用内联脚本设置变量:

<h3 id="c_down">剩余时间 <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script>
let minutes = <%=minutes%>;
let seconds = <%=seconds%>;
let hours = <%=hours%>;
</script>
<script src="countdown.js"></script>

因此从 countdown.js 中移除以下代码块:

let minutes=
let seconds = 
let hours=
英文:

You can set variables with inline script:

&lt;h3 id=&quot;c_down&quot;&gt;TIME LEFT &lt;br&gt; &lt;%=hours%&gt; : &lt;%=minutes%&gt; : &lt;%=seconds%&gt; &lt;/h3&gt;
&lt;script&gt;
let minutes = &lt;%=minutes%&gt;;
let seconds = &lt;%=seconds%&gt;;
let hours = &lt;%=hours%&gt;;
&lt;/script&gt;
&lt;script src=&quot;countdown.js&quot;&gt;&lt;/script&gt;

So remove this block from countdown.js:

let minutes=
let seconds = 
let hours= 

huangapple
  • 本文由 发表于 2020年8月21日 05:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63513177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定