“if()”中的Javascript多个ID

时间:2015-12-15 17:42:21

标签: javascript html

我想选择两个ID,但我不知道该怎么做。
我添加了humane.js,我想显示两个字段的错误消息。

感谢。

这是我的代码:

var flatty1 = humane.create({
  baseCls: 'humane-flatty',
  addnCls: 'humane-flatty-success',
  timeout: 2500
});
var flatty2 = humane.create({
  baseCls: 'humane-flatty',
  addnCls: 'humane-flatty-error',
  timeout: 2500
});

function check() {
  if (document.getElementById('name').value == 0) {
    flatty2.log("Please fill all fields first!");
  } else {
    flatty1.log("<b>" + document.getElementById('name').value + "</b>" + " is awesome name!");
    flatty1.log("<b>" + document.getElementById('country').value + "</b>" + " is a beautiful place!");
    flatty1.log("Yor name and country: " + "<b>" + document.getElementById('name').value + "</b>" + ", " + "<b>" + document.getElementById('country').value) + "</b>";
    flatty1.log("Thanks for testing this script " + "<b>" + document.getElementById('name').value + "</b>" + "!");
  }
}
html,
body {
  min-height: 100%;
}
.humane,
.humane-flatty {
  position: fixed;
  -moz-transition: all 0.4s ease-in-out;
  -webkit-transition: all 0.4s ease-in-out;
  -ms-transition: all 0.4s ease-in-out;
  -o-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
  z-index: 100000;
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
}
.humane,
.humane-flatty {
  font-family: Helvetica Neue, Helvetica, san-serif;
  font-size: 16px;
  top: 0;
  left: 30%;
  opacity: 0;
  width: 40%;
  color: #444;
  padding: 10px;
  text-align: center;
  background-color: #fff;
  -webkit-border-bottom-right-radius: 3px;
  -webkit-border-bottom-left-radius: 3px;
  -moz-border-bottom-right-radius: 3px;
  -moz-border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
  border-bottom-left-radius: 3px;
  -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  -moz-transform: translateY(-100px);
  -webkit-transform: translateY(-100px);
  -ms-transform: translateY(-100px);
  -o-transform: translateY(-100px);
  transform: translateY(-100px);
}
.humane p,
.humane-flatty p,
.humane ul,
.humane-flatty ul {
  margin: 0;
  padding: 0;
}
.humane ul,
.humane-flatty ul {
  list-style: none;
}
.humane.humane-flatty-info,
.humane-flatty.humane-flatty-info {
  background-color: #3498db;
  color: #FFF;
}
.humane.humane-flatty-success,
.humane-flatty.humane-flatty-success {
  background-color: #18bc9c;
  color: #FFF;
}
.humane.humane-flatty-error,
.humane-flatty.humane-flatty-error {
  background-color: #e74c3c;
  color: #FFF;
}
.humane-animate,
.humane-flatty.humane-flatty-animate {
  opacity: 1;
  -moz-transform: translateY(0);
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  -o-transform: translateY(0);
  transform: translateY(0);
}
.humane-animate:hover,
.humane-flatty.humane-flatty-animate:hover {
  opacity: 0.7;
}
.humane-js-animate,
.humane-flatty.humane-flatty-js-animate {
  opacity: 1;
  -moz-transform: translateY(0);
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  -o-transform: translateY(0);
  transform: translateY(0);
}
.humane-js-animate:hover,
.humane-flatty.humane-flatty-js-animate:hover {
  opacity: 0.7;
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=70);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/humane-js/3.2.2/humane.min.js"></script>

<a style="color: red;">*</a><a>Fill all fields and press "check" button.</a>
<br>
<input type="textbox" id="name" placeholder="Your name">
<input type="textbox" id="country" placeholder="Your country">
<button onclick="check()" style="background: white; color: black; border: 2px solid black; cursor: pointer;">check</button>

尝试在“名称”框中输入内容并将“国家/地区”框保留为空。

2 个答案:

答案 0 :(得分:2)

您可以选择这两个字段并测试它们的值是否为空字符串。如果其中任何一个为空,则显示错误。

您可以使用OR运算符||来实现此目的。

function check() {
    if (document.getElementById('name').value == '' || 
        document.getElementById('country').value == '') {
        flatty2.log("Please fill all fields first!");
    } else {
        // the rest of the thing    
    }
}

答案 1 :(得分:1)

我看到有一个公认的答案。我只想添加这个答案,说有更好的方法来进行验证。 OP可以向需要验证的所有字段添加类。然后验证这些字段。如果条件,则不会在每个字段新条目中创建此值。

<强> JS:

var flag = false;
var validateFields = document.getElementsByClassName("validate");
  for (var i = 0; i < validateFields.length; i++) {
    if (validateFields[i].value == '' || validateFields[i].value == undefined) {
      flatty2.log("Please fill all fields first!");
      flag = true;
    }
  }
  if (flag) {
    //success code
}

需要验证的字段:

<input type="textbox" id="country" class="validate" placeholder="Your country">

示例代码段

var flatty1 = humane.create({
  baseCls: 'humane-flatty',
  addnCls: 'humane-flatty-success',
  timeout: 2500
});
var flatty2 = humane.create({
  baseCls: 'humane-flatty',
  addnCls: 'humane-flatty-error',
  timeout: 2500
});

function check() {
  var flag = false;
  var validateFields = document.getElementsByClassName("validate");
  for (var i = 0; i < validateFields.length; i++) {
    if (validateFields[i].value == '' || validateFields[i].value == undefined) {
      flatty2.log("Please fill all fields first!");
      flag = true;
    }
  }
  if (flag) {
    flatty1.log("<b>" + document.getElementById('name').value + "</b>" + " is awesome name!");
    flatty1.log("<b>" + document.getElementById('country').value + "</b>" + " is a beautiful place!");
    flatty1.log("Yor name and country: " + "<b>" + document.getElementById('name').value + "</b>" + ", " + "<b>" + document.getElementById('country').value) + "</b>";
    flatty1.log("Thanks for testing this script " + "<b>" + document.getElementById('name').value + "</b>" + "!");
  }
}
html,
body {
  min-height: 100%;
}
.humane,
.humane-flatty {
  position: fixed;
  -moz-transition: all 0.4s ease-in-out;
  -webkit-transition: all 0.4s ease-in-out;
  -ms-transition: all 0.4s ease-in-out;
  -o-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
  z-index: 100000;
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
}
.humane,
.humane-flatty {
  font-family: Helvetica Neue, Helvetica, san-serif;
  font-size: 16px;
  top: 0;
  left: 30%;
  opacity: 0;
  width: 40%;
  color: #444;
  padding: 10px;
  text-align: center;
  background-color: #fff;
  -webkit-border-bottom-right-radius: 3px;
  -webkit-border-bottom-left-radius: 3px;
  -moz-border-bottom-right-radius: 3px;
  -moz-border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
  border-bottom-left-radius: 3px;
  -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  -moz-transform: translateY(-100px);
  -webkit-transform: translateY(-100px);
  -ms-transform: translateY(-100px);
  -o-transform: translateY(-100px);
  transform: translateY(-100px);
}
.humane p,
.humane-flatty p,
.humane ul,
.humane-flatty ul {
  margin: 0;
  padding: 0;
}
.humane ul,
.humane-flatty ul {
  list-style: none;
}
.humane.humane-flatty-info,
.humane-flatty.humane-flatty-info {
  background-color: #3498db;
  color: #FFF;
}
.humane.humane-flatty-success,
.humane-flatty.humane-flatty-success {
  background-color: #18bc9c;
  color: #FFF;
}
.humane.humane-flatty-error,
.humane-flatty.humane-flatty-error {
  background-color: #e74c3c;
  color: #FFF;
}
.humane-animate,
.humane-flatty.humane-flatty-animate {
  opacity: 1;
  -moz-transform: translateY(0);
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  -o-transform: translateY(0);
  transform: translateY(0);
}
.humane-animate:hover,
.humane-flatty.humane-flatty-animate:hover {
  opacity: 0.7;
}
.humane-js-animate,
.humane-flatty.humane-flatty-js-animate {
  opacity: 1;
  -moz-transform: translateY(0);
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  -o-transform: translateY(0);
  transform: translateY(0);
}
.humane-js-animate:hover,
.humane-flatty.humane-flatty-js-animate:hover {
  opacity: 0.7;
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=70);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Password test</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/humane-js/3.2.2/humane.min.js"></script>
</head>

<body>
  <a style="color: red;">*</a><a>Fill all fields and press "check" button.</a>
  <br>
  <input type="textbox" id="name" class="validate" placeholder="Your name">
  <input type="textbox" id="country" class="validate" placeholder="Your country">
  <button onclick="check()" style="background: white; color: black; border: 2px solid black; cursor: pointer;">check</button>
  <script>
  </script>
</body>

</html>