输入值无效时如何禁止焦点输出

时间:2013-08-25 04:04:59

标签: javascript jquery html

有一些文字输入字段(3+)如下:

A:__ B:__ C:__ 

当聚焦输入字段时,js将验证该值,我想它仍然在当前字段中聚焦,如果无效,但无法制作它,这是我的代码:

HTML:

<div class="input-short">
  <input type="text" data-mini="true" id="no1" name="no1" maxlength="2" placeholder="#1" />
</div>
<div class="input-short">
  <input type="text" data-mini="true" id="no2" name="no2" maxlength="2" placeholder="#2" />
</div>
<div class="input-short">
  <input type="text" data-mini="true" id="no3" name="no3" maxlength="2" placeholder="#3" />
</div>

CSS:

.input-short {
    float: left;
    width: 35px;
}

JS:

$('#no1').focusout(function(){
    if( $(this).val() == "" ||
       $(this).val() == $('#no2').val() ||
       $(this).val() == $('#no3').val() )
    {
        alert( "no" );
        $(this).focus();
    }
});
$('#no2').focusout(function(){
    if( $(this).val() == "" ||
       $(this).val() == $('#no1').val() ||
       $(this).val() == $('#no3').val() )
    {
        alert( "no" );
        $(this).focus();
    }
});

“$(this).focus();”不起作用,它将专注于下一个输入字段,如何只在输入值有效时才允许它聚焦?

0 个答案:

没有答案