英文:
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;
<h3 id="c_down">TIME LEFT <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script src="countdown.js"></script>
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('c_down');
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 <= 9 ? "0"+seconds : seconds;
minutes=minutes <= 9 ? "0"+minutes : minutes;
hours=hours <= 9 ? "0"+hours : hours;
cd.innerHTML = `TIME LEFT <br> ${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:
<h3 id="c_down">TIME LEFT <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script>
let minutes = <%=minutes%>;
let seconds = <%=seconds%>;
let hours = <%=hours%>;
</script>
<script src="countdown.js"></script>
So remove this block from countdown.js
:
let minutes=
let seconds =
let hours=
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论