让一个输入字段自动更新另一个输入字段,反之亦然(可能使用 JavaScript)

发布于 2024-12-25 03:32:51 字数 198 浏览 1 评论 0原文

我在 JavaScript 方面经验不足,因此需要一些项目帮助。

我有一个表单,其中包括 3 个输入文本字段(我们称它们为 A、B 和 C)。我需要用户只能输入 A+B OR C。因此,一旦用户单击另一个表单字段(反之亦然),是否可以使表单字段处于非活动状态。

非常感谢任何帮助。我的猜测是这不需要在 JavaScript 中完成。

I'm not that experienced in JavaScript, so need som help with a project.

I have a form of, among other things, 3 input text fields (let's call them A, B and C). I need the user to only be able to type into either A+B OR C. So is it possible to make a form field inactive, once the user clicks in another (and vice versa).

Would really appreciate any help. My guess is this needs t be done in JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2025-01-01 03:32:51

HTML

<input class="onlyone" id="first" />
<input class="onlyone" id="second" />
<input class="onlyone" id="third" />

JavaScript

document.querySelectorAll('.onlyone').addEventListener('click', function(){
if(this.getAttribute('disabled') != 'disabled'){ // check if it was disabled before

   for(var i=0; i<document.querySelectorAll('.onlyone').length; i++){
    document.querySelectorAll('.onlyone')[i].setAttribute('disabled, 'disabled'); //disable em all
   }

   this.removeAttribute('disabled'); //enable this one
}
}, false);

HTML

<input class="onlyone" id="first" />
<input class="onlyone" id="second" />
<input class="onlyone" id="third" />

JavaScript

document.querySelectorAll('.onlyone').addEventListener('click', function(){
if(this.getAttribute('disabled') != 'disabled'){ // check if it was disabled before

   for(var i=0; i<document.querySelectorAll('.onlyone').length; i++){
    document.querySelectorAll('.onlyone')[i].setAttribute('disabled, 'disabled'); //disable em all
   }

   this.removeAttribute('disabled'); //enable this one
}
}, false);
追我者格杀勿论 2025-01-01 03:32:51

您可以为此编写一个 JavaScript,如下所示进行绑定

<script>
function fun(ele){
var elea=document.getElementById('a');
var eleb=document.getElementById('b');
var elec=document.getElementById('c');
if (ele.id='a' &&(eleb.length==0 || elec.length==0))
{
      this.readonly=false;
}
if (ele.id='b' && (elea.length==0 || elec.length==0))
{
       this.readonly=false;
}
if (ele.id='c' && (elea.length==0 || eleb.length==0))
{
       this.readonly=false;
}

}
</script>
<input type="text" id="a" onfocus="fun(this)" readonly="true"/>
<input type="text" id="b" onfocus="fun(this)" readonly="true"/>
<input type="text" id="c" onfocus="fun(this)" readonly="true"/>

您还可以动态绑定事件而不是内联,请参阅 Javascript 绑定事件监听器的四种方式

you can write a javascript for this which is bind as follow

<script>
function fun(ele){
var elea=document.getElementById('a');
var eleb=document.getElementById('b');
var elec=document.getElementById('c');
if (ele.id='a' &&(eleb.length==0 || elec.length==0))
{
      this.readonly=false;
}
if (ele.id='b' && (elea.length==0 || elec.length==0))
{
       this.readonly=false;
}
if (ele.id='c' && (elea.length==0 || eleb.length==0))
{
       this.readonly=false;
}

}
</script>
<input type="text" id="a" onfocus="fun(this)" readonly="true"/>
<input type="text" id="b" onfocus="fun(this)" readonly="true"/>
<input type="text" id="c" onfocus="fun(this)" readonly="true"/>

you can also bind the events dynamically instead of inline see Four Ways Javascript Binding Event Listeners

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文