Javascript 验证后,单击返回错误文档事件不起作用

Javascript 验证后,单击返回错误文档事件不起作用,javascript,jquery,validation,Javascript,Jquery,Validation,我的网页中有以下验证: $( document ).on( 'click', '.click_button', function() { if(($('#first_name').val() == "")){ alert('Firstname missing'); return false; } // post to ajax }); 一旦遇到firstname缺少验证错误,单击事件(对于类单击按钮)随后将不起作用。返回false后如何

我的网页中有以下验证:

$( document ).on( 'click', '.click_button', function() {
    if(($('#first_name').val() == "")){
        alert('Firstname missing');
        return false;
    }
    // post to ajax 
});
一旦遇到firstname缺少验证错误,单击事件(对于类
单击按钮
)随后将不起作用。返回false后如何追加click事件。如有任何建议,将不胜感激

function checkFormIsValid(){
  var isValid = true;
  var input = $.trim($('#first_name').val());
  if(typeof input != 'string' || input==''){
        isValid = false;
  }
  return isValid;
}

$( document ).on( 'click', '.click_button', function(e) {
  if(checkFormIsValid()){
    //do your stuff
  }else{
     e.preventDefault();
     alert('Firstname missing');
     return false;
  }
});
这是小提琴

这是小提琴

请看一下(基本上我没有添加任何内容。只有$('first#u name').val()。你的代码正在运行

html

请回顾这篇文章(基本上我没有添加任何内容。只有$(“#first_name”).val()。你的代码正在运行

html


jQuery对象永远不会等同于空字符串,因此您的
如果
条件有缺陷。你是说
$('#first_name').val()
吗?不清楚为什么要在
返回false
之后应用click事件,在返回之前尝试使用setTimeout,它会返回,在超时结束后,它会做任何你想做的事情。jQuery对象永远不会等于空字符串,因此,如果
条件有缺陷,您的
。你是说
$('#first_name').val()
吗?不清楚为什么要在
return false
之后应用click事件,在返回之前尝试使用setTimeout,它会返回,在超时结束后,它会做任何你想做的事情。
<input id="first_name">
<button type="button" class="click_button">post</button>
$(document).on('click', '.click_button', function(e) {
  if (($('#first_name').val() === "")) {
    alert('Firstname missing');
    return false;
  }
  alert('sending');
  // post to ajax 
});