[英]How to add value of variable at the end of URL in JavaScript?
我有一个counter
按钮,可以在点击时增加数量。
我也有一个像http://localhost:5000/x/
的URL。
我想要的是:
打印URL末尾的counter
值。
例如:
如果counter = 3,
我想去http://localhost:5000/x/3
;
我的按钮类型是submit
,它增加了计数器的值,并进入页面http://localhost:5000/x/counter
。 但是,我得到404错误,因为数据在http://localhost:5000/x/3
。 这是代码。
<form action="http://localhost:5000/x/counter">
<script>
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
谢谢。另外我不使用任何框架,我想通过使用JavaScript来处理它。
编辑:谢谢你的回答。 我通过使用window.location.href
函数解决了它。
使用页面的URL检索计数器
var counter = location.href.substring(location.href.lastIndexOf('/')+1);
counter = parseInt(counter);
counter += 1;
使用计数器设置myform操作
document.myform.action = location.href.substring(0,location.href.lastIndexOf('/'))+counter;
为表单命名
<form name='myform'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.