简体   繁体   English

我如何转移已输入的值 <input number> 对象转换成javascript而不刷新页面?

[英]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.

相关问题 在没有客户端脚本的情况下刷新页面后如何重置隐藏的输入值? - How can i reset hidden input value after refreshing page without client side scripting? 如何在不刷新页面的情况下重置输入值 - How to reset the input value without refreshing the page 如果数据属性(HTML)=== javascript 中的输入文本,如何更改已写入输入的每个字母的 javascript 颜色? - How can I change color in javascript for each letter which has been written into an input if data attribute(HTML) === input text in javascript? 如何在输入字段中输入字符时如何判断? - How can I tell when a character has been typed in an input field? JavaScript-保存输入值并随后清除输入,以便无需刷新页面即可重复使用 - JavaScript - saving input value and cleaning the input afterwards so it can be reused without refreshing page 如何让密码点出现在 javascript 中除了最后一个之外已经输入的那些字母? - how can I make the password dots appear of those letters which have been typed except last one in javascript? 设置输入字段的值而无需刷新页面 - Setting the value of an input field without refreshing the page 如何在不刷新整个页面的情况下使用javascript自动更新新闻提要? - How can I auto-update a news feed with javascript without refreshing the whole page? JavaScript - 如何知道哪个对象被点击 - JavaScript - How to know which object has been clicked 如何对用户以 html 形式输入的数据应用字符串化? - How can i apply stringify on the data which has been input by the user in an html form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM