简体   繁体   English

填写第一个文本框后提交的Javascript按钮不会更改下一个文本框的颜色

[英]Javascript button submit after first textbox is filled in does not change color of next textbox

I have found help here before and I am totally new to coding in general. 我以前在这里找到了帮助,而且我对编码一般都是新手。 I am writing a Javascript script to change colours when I insert text. 我正在编写一个Javascript脚本来在插入文本时更改颜色。 When there is text, the first one goes red. 当有文本时,第一个变为红色。 I want all of them to go to red. 我希望他们所有人都变红了。 And when they are filled in they must go blue one by one. 当他们被填满时,他们必须一个接一个地变蓝。 I have used an if statement for this. 我为此使用了if语句。 Should I rather look at a different method? 我应该考虑一种不同的方法吗?

function btnSubmit(input) {
    var name = document.getElementById('name');
    var email = document.getElementById('email');
    var message = document.getElementById('message');

    if (name.value == '') {

      name.style.borderColor = 'red';
      return false;

      // stop submission until textbox is not ''
    }else{
      name.style.borderColor = 'blue';
      return false;
    }

    if (email.value == '') {
      email.style.borderColor = 'red';
      return false;
    }else{
      email.style.borderColor = 'blue';
      return false;
    }

    if (message.value == '') {
      message.style.borderColor = 'red';
      return false;
    }else{
      message.style.borderColor = 'blue';
      return false;
    }

    if (name.value != '' && email.value != '' && message.value != ''){
      alert('all fields filled in');
      return true;
    }
} 

You should not return false in else statements, return statement stops function execution and all lines after return statement will not get executed. 你不应该在else语句中返回false,return语句会停止函数执行,并且return语句之后的所有行都不会被执行。 In your code, function btnSubmit will always check name field only as you are have return in both if and else conditions. 在您的代码中,函数btnSubmit将始终只检查名称字段,因为您在if和else条件下都返回。

You code should be like 你的代码应该是这样的

function btnSubmit(input) {
    var name = document.getElementById('name');
    var email = document.getElementById('email');
    var message = document.getElementById('message');

    if (name.value == '') {

      name.style.borderColor = 'red';
      return false;
    }else{
      name.style.borderColor = 'blue';
    }

    if (email.value == '') {
      email.style.borderColor = 'red';
      return false;
    }else{
      email.style.borderColor = 'blue';
    }

    if (message.value == '') {
      message.style.borderColor = 'red';
      return false;
    }else{
      message.style.borderColor = 'blue';
    }

    // No need to add this condition as you have already checked all three fields individually
    //if (name.value != '' && email.value != '' && message.value != ''){
      alert('all fields filled in');
      return true;
    //}
}

I'll just show you an example, how to iterate through all your input tags and apply blue or red style to them based on content. 我将向您展示一个示例,如何遍历所有input标记,并根据内容将蓝色或红色样式应用于它们。 It's a more proper way than declaring every input inside your function. 这比声明函数内的每个输入更合适。 I think it's a good point to start, so you need to modify this code a little to get what you want: 我认为这是一个很好的开始,所以你需要修改一下这个代码以获得你想要的东西:

 function btnSubmit(input) { var inputs = document.getElementsByTagName('input') var counter = 0; for (var i = 0; i < inputs.length; i++) { if (inputs[i].value == '') { inputs[i].style.borderColor = 'red'; } else { counter++ inputs[i].style.borderColor = 'blue'; } } if (inputs.length === counter) { alert('all fields filled in'); } } 
 <input id="name"> <input id="email"> <input id="message"> <button onclick="btnSubmit()">Submit</button> 

 var name = document.getElementById('name'); var email = document.getElementById('email'); var message = document.getElementById('message'); var inputs = [name, email, message]; inputs.forEach(function(input) { input.addEventListener('change', check); input.addEventListener('focusout', check); }); function check() { if (this.value.length === 0) { inputs.forEach(function(el) { if (el.value.length === 0) el.style.borderColor = 'red' }); } else { this.style.borderColor = 'blue'; } } 
 <input id="name" type="text"> <input id="email" type="text"> <input id="message" type="text"> 

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

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