checking if value is null using jQuery

This question already has an answer here:

  • jQuery: checking if the value of a field is null (empty) 6 answers

  • The .val() always returns a string . You can check if the string is empty using:

    .val().trim().length == 0
    

    So your full condition will be:

    var form_intitule = $('input[name=form_intitule]').val().trim();
    
    if (form_intitule.length == 0) {
      alert('fill the blank');
      return false;
    }
    

    我会建议一个更安全的方法

    var formElement = $('input[name=form_intitule]');
    if ($(formElement).length == 0 || $(formElement).val().trim().length == 0) {
       console.log("element does not exist OR value is empty");
    }
    
    链接地址: http://www.djcxy.com/p/76366.html

    上一篇: jQuery条件链接

    下一篇: 使用jQuery检查值是否为null