Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么.focusout在.submit调用-jquery时_Javascript_Jquery - Fatal编程技术网

Javascript 为什么.focusout在.submit调用-jquery时

Javascript 为什么.focusout在.submit调用-jquery时,javascript,jquery,Javascript,Jquery,我想在用户不关注输入和提交表单时向他们提供表单反馈。当我只有focusout方法时,它就工作了,但当我添加submit方法时,它就失败了 (function(){ $('input').focusout(function(){ if($(this).val() == ""){ $('.jsError').html('<p class="alert alert-danger text-center">'+$(thi

我想在用户不关注输入和提交表单时向他们提供表单反馈。当我只有focusout方法时,它就工作了,但当我添加submit方法时,它就失败了

(function(){
        $('input').focusout(function(){
            if($(this).val() == ""){
                $('.jsError').html('<p class="alert alert-danger text-center">'+$(this).attr('name')+' is required.</p>'); 
            }
        });

        //when the submit button is pressed
        $("#register").click(function(event){
            //var input = $(this).parent().find('input[value=""]');
            if(!input.length){
                event.preventDefault();
                $('.jsError').html('<p class="alert alert-danger text-center">'+Please fill in the form+'</p>');
            }else{
                //set variables
                var email = $('#email'),
                    emailConf = $('#emailConf'),
                    pass = $('#pass'),
                    passConf = $('#passConf'),
                    stageName = $('#stageName'),
                    mainClub = $('$mainClub'),
                    mainSkill = $('#ent_main_skill'),
                    termsAgree = $('#agreeToTerms');
            }
        }); 
})();
(函数(){
$('input').focusout(函数(){
if($(this).val()==“”){
$('.jsError').html('

'+$(this.attr('name')+'是必需的。

'); } }); //当按下提交按钮时 $(“#注册”)。单击(函数(事件){ //var input=$(this.parent().find('input[value=”“]); 如果(!input.length){ event.preventDefault(); $('.jsError').html('

“+请填写表格+”

); }否则{ //设置变量 var email=$(“#email”), emailConf=$(“#emailConf”), 通过=$(“#通过”), passConf=$(“#passConf”), stageName=$(“#stageName”), mainClub=$(“$mainClub”), 主要技能=$('ent#u主要技能'), termsAgree=$(“#agreeToTerms”); } }); })();

我认为这是event.preventdefault,但它本身似乎没有任何作用。

从我看到的注释行来看,您试图查询所有没有值的输入。这意味着如果
input.length
大于
0
,则存在错误,但如果
0
,则表示所有表单字段都已填充

您实现的逻辑正好相反,应该是:

if (input.length) {
    //errors
} else {
   //no errors
}

你的逻辑应该是相反的不?如果
input.length
为truthy,则表示由于查询没有值的元素,因此出现了一些错误。这可能解释了为什么“防止默认值”不起作用,但也解释了为什么在添加submit时“取消焦点”停止起作用method@user2251229那么,在添加click listener时,控制台中是否有JS错误?使您取消注释定义
输入的代码行。如果它不起作用,请设置一个JSFIDLE示例。@user2251229好吧,实时协作不是很有用,除非我们都在那里;)你能安装一个普通的JSFIDLE吗?听起来不错,所以我现在正在工作,所以大约两个小时后就可以发布了。谢谢你的反馈