简体   繁体   English

使用Javascript更新输入框中的文本

[英]Update Text in input box using Javascript

I have an input box which is written as shown below: 我有一个输入框,其编写如下:

<input id="SlidingWindow" type="number" placeholder="50" class="span3">

This input box is part of a form and on clicking a submit button, I call a javascript function. 此输入框是表单的一部分,在单击提交按钮时,我调用了一个javascript函数。 I am trying to do a check if the number inputted is withing range. 我正在尝试检查输入的数字是否在范围内。 Incase it is not in range, I want to set the value in the box to a default value as shown below: 如果它不在范围内,我想将框中的值设置为默认值,如下所示:

slidingWindow = document.getElementById("SlidingWindow").value;
    if (slidingWindow < 30) {
        slidingWindow = 30;     
    } else if (slidingWindow > 2000) {
        slidingWindow = 2000;       
    }
    document.getElementById("SlidingWindow").innerHTML = slidingWindow;

However on doing this, the text in the box does not change. 但是,执行此操作时,框中的文本不会更改。 Instead of innerHTML I tried value as well. 我没有使用innerHTML,而是尝试了价值。 That did not work either. 那也行不通。 What could be the possible solution? 可能的解决方案是什么?

The problem here is that you are using .innerHTML to change the value of an input which is incorrect, you have to use .value instead, and to fire this test wrap your code into a function and attach it to the onchange event of your input , like this: 这里的问题是你使用.innerHTML来改变输入的值是不正确的,你必须使用.value ,然后触发这个测试将你的代码包装到一个函数中并将它附加到你inputonchange事件, 像这样:

 document.getElementById("SlidingWindow").onchange = function() { slidingWindow = document.getElementById("SlidingWindow").value; if (slidingWindow < 30) { slidingWindow = 30; } else if (slidingWindow > 2000) { slidingWindow = 2000; } document.getElementById("SlidingWindow").value = slidingWindow; } 
 <input id="SlidingWindow" type="number" placeholder="50" class="span3"> 

You can test the Working Fiddle here . 你可以这里测试工作小提琴

Replace innerHTML by value value替换innerHTML

 function foo() { slidingWindow = document.getElementById("SlidingWindow").value; if (slidingWindow < 30) { slidingWindow = 30; } else if (slidingWindow > 2000) { slidingWindow = 2000; } document.getElementById("SlidingWindow").value = slidingWindow; } 
 <input id="SlidingWindow" type="number" placeholder="50" class="span3" onchange="foo()"> 

You have do one change only 您只做了一次更改

 document.getElementById("SlidingWindow").innerHTML = slidingWindow;

replace this line by 将此行替换为

document.getElementById("SlidingWindow").value= slidingWindow;

Other way is you can do this calling a function onchange 另一种方法是你可以这样做调用onchange函数

<input id="SlidingWindow" type="number" placeholder="50" class="span3" onchange="myFunction()">

function myFunction() {
    slidingWindow = document.getElementById("SlidingWindow").value;
    if (slidingWindow < 30) {
        slidingWindow = 30;     
    } else if (slidingWindow > 2000) {
        slidingWindow = 2000;       
    }
    document.getElementById("SlidingWindow").value = slidingWindow;
  }

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

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