简体   繁体   English

按Tab键时的Java脚本形式验证

[英]java-script form validation when press tab key

I faced a problem.I have total 4 text-box fields and a submit button each box are required. 我遇到了一个问题,我总共有4个文本框字段和每个框都需要一个提交按钮。 But i want to do, when press tab key it does not need jump to second field if those field are empty,give error with red color border otherwise jump to second text field. 但是我想这样做,当按Tab键时,如果这些字段为空,则无需跳转到第二个字段,否则会出现红色边框,否则跳转到第二个文本字段。 [JSFidle][1]

Try like this 这样尝试

Js Fiddle 提斯

$('input').keydown(function(e) {
    var code = e.keyCode || e.which;
    $(this).css('border','');
    if (code == '9') {        
        if($(this).val()==''){
             $(this).next().html('this field is required!');
             $(this).css('border','1px solid red');
            $( this ).focus();
            return;
        }           
    }
 });

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

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