类似的方法为空白点发出警告信息?

时间:2018-05-22 10:40:49

标签: javascript

如果没有引入号码,则应显示一条消息,要求用户输入至少一个号码。

我想知道是否有其他方法可以做到这一点?如果我输入b(num2)c(num3)的值并且我不输入a(num1),我会从结尾处获得该警告弹出窗口

Javascript

function max(){
    var a=parseInt(document.getElementById("num1").value);
    var b=parseInt(document.getElementById("num2").value);
    var c=parseInt(document.getElementById("num3").value);
        if(a>b && a>c){
            alert(num1.value);
            }else if(b>a && b>c){
            alert(num2.value);
            }else if(c>a && c>b){
                alert(num3.value);
            }else if(a>b && a>c){
                alert(num1.value);
            }else if(a>b){
                alert(num1.value);
            }else if(a>c){
                alert(num1.value);
                return;
            }else if(b>a){
                alert(num2.value);
            }else if(b>c){
                alert(num2.value);
            }else if(c>a){
                alert(num3.value);
            }else if(c>b){
                alert(num3.value);
            }else if(a){
                alert(num1.value);
            }else if(b){
                alert(num2.value);
            }else if(c){
                alert(num3.value);
            }

        if (document.getElementById("num1").value == "") {
                alert("Input at least one number");
            }
    }

HTML

<input type="text" id="num1" >a </br><br>
<input type="text" id="num2" >b </br><br>
<input type="text" id="num3" >c </br><br>
<button onclick="max()">"Click me !</button>

3 个答案:

答案 0 :(得分:2)

&#13;
&#13;
function max(){
var a=parseInt(document.getElementById("num1").value);
var b=parseInt(document.getElementById("num2").value);
var c=parseInt(document.getElementById("num3").value);

//exclude NaNs
nbs = [a,b,c].filter(nb => !isNaN(nb));

  if (isNaN(a) && isNaN(b) && isNaN(c)) {
    alert("Input at least one number");
  }
  else {
    console.log(Math.max(...nbs));
  }
}
&#13;
<input type="text" id="num1" >a </br><br>
<input type="text" id="num2" >b </br><br>
<input type="text" id="num3" >c </br><br>
<button onclick="max()">"Click me !</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

@Leny Lestter,你可以使用下面的jquery函数

var empty = 0;
      $('input[type=text]').each(function(){
         if (this.value == "") {
             alert(' empty input' +$(this).attr("id"))
         } 
      })

答案 2 :(得分:0)

我希望这能解决你的问题。

&#13;
&#13;
function max(){
  // checking rhe three fields have value
  var inputs = $("body :input:not(:checkbox):not(:button)");
  var emptyfields = inputs.filter(function () {
    return $.trim($(this).val()).length == 0
     }).length == 0;
  if(emptyfields){
    // fields are not empty you can write code here
  }else{
    alert("Input fields can't be empty")
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="num1" >a </br><br>
<input type="text" id="num2" >b </br><br>
<input type="text" id="num3" >c </br><br>
<button onclick="max()">"Click me !</button>
&#13;
&#13;
&#13;

相关问题