简体   繁体   English

jQuery检查输入是否为空

[英]jQuery check if input is empty

I am trying to get it where, if there is value in the input, go ahead and do your thing but if there isn't, don't do anything. 我正在尝试把它放在哪里,如果输入中有价值,那就继续做您的事情,但是如果没有,就什么也不要做。 I keep getting getting it where, if there is nothing in the input, a failure message occurs but only if I hit the enter key 我不断得到它,如果输入中没有任何内容,则会出现失败消息,但仅当我按Enter键时
jsfiddle.net/BBaughn/8ovrmhgp click the space in the lower right corner, then press enter. jsfiddle.net/BBaughn/8ovrmhgp单击右下角的空格,然后按Enter。 It shouldn't pop up, because the input is not focused. 它不应该弹出,因为输入没有集中。

login/failure jQuery: 登录/失败jQuery:

$(document).ready(function(){
  $('.btn1').on('click', function(){
    var login = [marion, 'FATHER1'];

    var marion = $('#logging').val();
    marion = marion.toUpperCase();

    if (marion !== 'FATHER1' && $('#logging').val()) {
      alert('Login Failed');
  } else if (marion === 'FATHER1' && $('#logging').val()) {
    $('.notify').css('margin-top', '0');
      $('#logging').val('');
  }

  });
  $('.exit').on('click', function(){
    $('.notify').slideUp('slow');
  });
});

if you just want to check if an input is empty, I'm guessing #logging is the input: 如果您只是想检查输入是否为空,我猜想#logging是输入:

$('.btn1').on('click', function(){
    var marion = $('#logging').val();
    if (marion == '') {
       //this means the input value was empty
    }
});

Ok, I think I get what you mean now. 好吧,我想我明白你的意思了。 You're checking the entire document for keypresses as is, you need to check the input only. 您正在按原样检查整个文档中的按键,只需要检查输入即可。 I think this is a good solution to do that: 我认为这是一个很好的解决方案:

$('.login input').keypress(function (e) {
    if (e.which == 13 && !$('#logging').val()) {
        e.PreventDefault();
    } else if (e.which == 13 && $('.btn1').focus()) {
        $('.btn1').click();
    }
});

Working Fiddle here 在这里工作小提琴

if the length is equal zero, that means the field is empty. 如果长度等于零,则表示该字段为空。 It works after adding length === 0 check, please try this, 添加length === 0检查后可以使用,请尝试此操作,

if (e.which == 13 && $('.btn1').val().length === 0) {
    e.preventDefault();
} else if (e.which == 13 && $('.btn1').focus()) {
    $('.btn1').click();
}
$(document).ready(function(){
  $('.btn1').on('click', function(){
    var login = [marion, 'FATHER1'];

    var marion = $('#logging').val();
    marion = marion.toUpperCase();

    if (marion !== 'FATHER1' && $('#logging').val()) {
      alert('Login Failed');
  } else if (marion === 'FATHER1' && $('#logging').val()) {
    $('.notify').css('margin-top', '0');
      $('#logging').val('');
  }

  });
  $('.exit').on('click', function(){
    $('.notify').slideUp('slow');
  });
});

I didn't realize that my if statement was if the input wasn't detecting the right login, so it didn't matter if it was out of focus. 我没有意识到我的if语句是否是输入未检测到正确的登录名,因此是否没有重点也没关系。 Now it says "if it's not FATHER1 and there is value in the input only, then send this alert" 现在它说“如果不是FATHER1并且仅在输入中有值,则发送此警报”

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

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