[英]How can i transfer value which has been typed in <input number> object into javascript without page refreshing?
I have problem with transferring value from the object to the javascript without page refresh. 我在不刷新页面的情况下将值从对象转移到javascript时遇到问题。 I have noticed, that if my page refresh, my value disappear.
我注意到,如果页面刷新,我的价值就会消失。
Fiddle example:
http://jsfiddle.net/pcgy3/185/ http://jsfiddle.net/pcgy3/185/
Could someone help me with this problem ? 有人可以帮我解决这个问题吗? Thank You in advance :)
先感谢您 :)
You don't technically need the form, you could just have the elements on their own. 从技术上讲,您不需要表格,您可以只拥有元素。 Does this answer the question you have?
这是否回答您的问题?
HTML: HTML:
Value(between 1 and 20):
<input type="number" id="num_of_balls" min="1" max="20">
<button id="clickBTN">Click</button>
JS: JS:
function onClick() {
var ballCount = parseInt(document.getElementById("num_of_balls").value, 10);
if(ballCount < 1 || ballCount > 20) return; // it have to be value between 1 and 20 to continue
var i = 0;
alert("#Balls: " + ballCount);
for (i = 0; i < ballCount; i++) {
obj_t.push({
xPos : 10 + 10 * i,
yPos : 10 + 11 * i,
xVel : 0 + i / 10,
yVel : 1 + i / 10,
r : 2 + i / 5
});
}
}
var obj_t = [];
var btn = document.getElementById("clickBTN");
btn.addEventListener("click", onClick);
document.getElementById("num_of_balls").addEventListener("keypress", (evt) => {
if(evt.key === "Enter") {
onClick();
}
});
You can a listener to the input change : 您可以监听输入更改:
var input = document.querySelector('input');
input.addEventListener('input', function()
{
obj_t=[];
for (var i = 0; i < document.getElementById('num_of_balls').value; i++) {
var obj = {
xPos : 10 + 10 * i,
yPos : 10 + 11 * i,
xVel : 0 + i / 10,
yVel : 1 + i / 10,
r : 2 + i / 5
}
obj_t.push(obj);
}
console.log(obj_t);
});
http://jsfiddle.net/pcgy3/188/ http://jsfiddle.net/pcgy3/188/
That is obvious, none of the changed made to the DOM stays once after the page gets reloaded, unless you are storing the the value somewhere in cookie, session, local-storage or DB. 显而易见,重新加载页面后,对DOM所做的任何更改都不会保留一次,除非您将值存储在Cookie,会话,本地存储或DB中的某个位置。
Though here is an example of getting the value in JS. 尽管这是在JS中获取值的示例。 Iam using JQuery .keyup as an event which will trigger the value from
我使用JQuery .keyup作为事件,它将触发来自
HTML: HTML:
<input type="number" id="num_of_balls" min="1" max="20">
JS JS
$('#num_of_balls').keyup(function(e){
if(e.which == 13) { // on Enter key pressed
alert(document.getElementById('num_of_balls').value)
}
})
Code here http://jsfiddle.net/pcgy3/189/ 代码在这里http://jsfiddle.net/pcgy3/189/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.