【问题标题】:validate required fields are filled on modal with jquery使用 jquery 在模态上填写必填字段
【发布时间】:2015-11-18 10:18:29
【问题描述】:

我在模态弹出窗口中动态填充一些文本框,提交表单时我需要检查必填字段是否已填写。

我尝试了两种方法,但都不起作用..

如果你想检查这里是完整的代码 http://jsfiddle.net/nsk21/f52nLfrj/

这是如何尝试1:

 $(document).on('click', '#catSave', function(){
    var countValid = 0 ;
    totRequFileds = 0;

    $('#catCreatForm').find('input').each(function(){
        alert($(this).attr('type'));
        if(!$(this).prop('required')){
            if ( this.value.trim() !== '' ) {                           
                countValid++;                                           
            }else{      
                $(this).focus();
                return false;
            }
            totRequFileds++; 
        } else {

        }
    });       
    alert(' totRequFileds:'+totRequFileds +' | '+countValid);
});

尝试 2:

      $( ':input[required]', '#catCreatForm' ).each( function () {
            alert($(this).attr('name'));
            if ( this.value.trim() !== '' ) {                           
                countValid++;                                           
            }else{      
                $(this).focus();
                return false;
            }
            totRequFileds++; 
        }); 

【问题讨论】:

标签: jquery required


【解决方案1】:

你可能没有注释掉小提琴中的那些行,我已经稍微改变了这些行

$(document).on('click', '#catSave', function(){
	var countValid = 0 ;
	totRequFileds = 0;
	$( ':input[required]', '#catCreatForm' ).each( function () {
		totRequFileds++;
		alert($(this).attr('name'));
		if ( this.value.trim() !== '' ) {							
			countValid++;											
		}else{		
			$(this).focus();
			//return false;
		}
	});
	if( countValid != totRequFileds){
		alert('Please fill out all required fileds totRequFileds:'+totRequFileds +' | '+countValid);
      return false;
	}
    alert(' totRequFileds: '+totRequFileds +' | '+countValid);
});

【讨论】:

  • 它不起作用,它只检查第一个文本框而不是所有文本框
  • 您在代码中所做的是一次检查每个框,如果一个框为空,则将焦点放在它上面并结束检查,,..您可以通过删除'return false;'在'$(this).focus()'下面
  • 我会改变它并通过取消行来告诉你
猜你喜欢
  • 2013-11-14
  • 2020-04-26
  • 2017-03-28
  • 2019-01-04
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
相关资源
最近更新 更多