简体   繁体   English

如何将输入元素中键入的值分配给变量?

[英]How to assign to a variable, the value typed in an input element?

I have this html element我有这个 html 元件

<label for="minV">Minimum value</label>
          <input
            class="inputCell"
            type="number"
            value="6"
            id="minV"
            onclick="this.select()"
          />

This is how I see it in the browser这就是我在浏览器中看到的方式

I changed the value from 6 to 10. I want to assign the new value to a variable in js.我将值从 6 更改为 10。我想将新值分配给 js 中的变量。 If I'm doing this:如果我这样做:

let min = Number(document.querySelector("#minV").value);

the 'min' variable will be always have value 6. 'min' 变量将始终具有值 6。

How can I assign to the variable 'min' the value that I have typed in the input element?如何将我在输入元素中键入的值分配给变量“min”?

Use the min attribute.使用min属性。 Additionally you can use a handler to assign a value (here to the global variable called min ) and keep that value above or equal to minimum on the value of the min-attribute.此外,您可以使用处理程序分配一个值(此处为名为min的全局变量)并保持该值高于或等于 min 属性值的最小值。 The snippet uses event delegation to handle stuff.该片段使用事件委托来处理事情。

 let min = 6; // <= always 6 or up // listen to keyup/mouseup/-down events document.addEventListener(`keyup`, handle); document.addEventListener(`mousedown`, handle); document.addEventListener(`mouseup`, handle); function handle(evt) { // only act on input#minV if (evt.target.id === `minV` && /\d|backspace|delete|up|down/i.test(evt.key) || /^mouse/i.test(evt.type)) { const inp = evt.target; const inpMin = inp.getAttribute(`min`); // min magic here min = inp.value < +inpMin? inpMin: inp.value; // display value for demo document.querySelector(`.minValue`).textContent = `Minumum value: ${min}`; } }
 <label for="minV">Minimum value</label> <input class="inputCell" type="number" value="6" id="minV" min="6" /> <p class="minValue">Minimum value: -</p>

Something like that will help?类似的东西会有帮助吗?

<label for="minV">Minimum value</label>
          <input
            class="inputCell"
            type="number"
            value="6"
            id="minV"
            onclick="this.select()"
            onchange="myFunction()"
          />

Script脚本

var min = 6
function myFunction(){
    min = Number(document.querySelector("#minV").value)
}

You need to use addEventListener to listen the input's value change您需要使用 addEventListener 来监听输入的值变化

For example:例如:

 let minVofInput = document.querySelector('#minV'); let min = 6; // initial value minVofInput.addEventListener('input', function() { min = minVofInput.value; // assign input's value to variable min })

If you only call set min once that won't do, you need to keep updating min every time the input changes.如果你只调用一次 set min就不行了,你需要在每次输入改变时不断更新min Create a function that does what you want and call it from the input's onchange property (or set up an event listener, whatever you like).创建一个 function 执行您想要的操作并从输入的onchange属性调用它(或设置一个事件侦听器,无论您喜欢什么)。

 let min = undefined; function onChange() { const input = document.querySelector("#minV"); min = input.value; console.log(min); }
 <label for="minV">Minimum value</label> <input class="inputCell" type="number" value="6" id="minV" onclick="this.select()" onchange="onChange()" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM