简体   繁体   English

JavaScript-如何在input和onchange上更改字段值

[英]JavaScript - how to change field values oninput and onchange

I can't figure out how to change form field values with the oninput or the onchange method. 我无法弄清楚如何使用oninputonchange方法更改表单字段值。 For example, I need to make one input field to change its value as soon as another input field is being changed. 例如,一旦另一个输入字段被更改,我需要使一个输入字段更改其值。 So I'm trying: 所以我正在尝试:

<input type="number" id="a" value="999.99" oninput="updateInput(value)"/>
<input type="number" id="b" value=""/>

<script>
    $(document).ready(function updateInput(value) {
        document.getElementById("b").value = document.getElementById("a").value;
    });
</script> 

It doesn't do anything. 它什么也没做。 What am I doing wrong? 我究竟做错了什么?

In another case, I need to change the value of a drop-down according to another drop-down (with an if clause). 在另一种情况下,我需要根据另一个下拉列表(使用if子句)更改下拉列表的值。 Since the simple script above doesn't work already, I don't even have a clue what to do about the second one... 由于上面的简单脚本尚无法使用,因此我什至不知道该如何处理第二个脚本...

There are a few issues here; 这里有一些问题。 the first one to sort out is where you're declaring your functions. 首先要解决的是声明函数的位置。 Javascript has a concept of scope , which you can think of as kind of a container for declared variables. JavaScript具有作用域的概念,您可以将其视为声明的变量的容器。 The reason your function isn't doing anything is because the definition of updateInput is locked away inside your jquery $(document).ready scope. 您的函数没有执行任何操作的原因是因为updateInput的定义被锁定在jquery $(document).ready范围内。

Once you take that definition out of the jquery wrapper, it's available to the global window scope - which is the one you're accessing in your oninput . 一旦从jquery包装器中取出该定义,它就可用于全局窗口作用域-这是您在oninput访问的oninput I should point out that there are more flexible/useful ways to put an event listener on something, but what you have will work, if this is a simple use case. 我应该指出,有一些更灵活/有用的方法可以将事件侦听器放置在某些对象上,但是如果这是一个简单的用例,那么您可以使用它。

 function updateInput(value) { document.getElementById("b").value = document.getElementById("a").value; } 
  <input type="number" id="a" value="999.99" oninput="updateInput(value)"/> <input type="number" id="b" value=""/> 

Your function is undefined in the markup, because it is passed to the ready and scoped there. 您的函数在标记中未定义,因为它传递给了ready并在其中作用域。 It will better to not pass the update function to the ready , but create inside it and attach that function to the input from code. 最好不要将update函数传递给ready ,而是在其内部创建并将该函数附加到代码的input Don't get every time the DOM elements, instead get once and use variables to keep them. 不要每次都获取DOM元素,而是获取一次并使用变量来保留它们。

Also you have mixed jQuery approach with pure Javascript approach. 另外,您将jQuery方法与纯Javascript方法混合使用。 I think it will be better to use one of them. 我认为使用其中之一会更好。

With jQuery 使用jQuery

 $(document).ready( function() { const aInput = $("#a"); const bInput = $("#b"); aInput.on('input', function () { bInput.val(aInput.val()); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" id="a" value="999.99"/> <input type="number" id="b" value=""/> 

With pure Javascript 使用纯Javascript

 window.onload = function() { const aInput = document.getElementById("a"); const bInput = document.getElementById("b"); aInput.addEventListener('input', function() { bInput.value = aInput.value; }); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" id="a" value="999.99"/> <input type="number" id="b" value=""/> 

You have to define the updateInput() function outside of $(document).ready() , so that you can call it from the rest of your code, otherwise it is only scoped in there. 您必须在$(document).ready()之外定义updateInput()函数,以便可以从其余代码中调用它,否则仅在其中作用域。

Secondly, the argument the function takes is never used, so I took the liberty of removing it. 其次,该函数采用的参数从未使用过,因此我可以随意删除它。

Thirdly, to call this function as soon as the page is loaded, just call it inside $(document).ready() and you are good to go. 第三,要在页面加载后立即调用此函数,只需在$(document).ready()内部调用它就可以了。

Here's a working example: 这是一个工作示例:

 function updateInput() { document.getElementById("b").value = document.getElementById("a").value; } $(document).ready(updateInput()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" id="a" value="999.99" oninput="updateInput()" /> <input type="number" id="b" value="" /> 

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

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