Jquery自动对焦在下一个字段上

时间:2014-10-30 15:03:15

标签: jquery asp.net-mvc jquery-ui jquery-plugins

我怎样才能在2位数之后在下一个字段上进行自动对焦?

这是jquery,它不适合我 -

    $("#personalDetailMobilePhoneAreaInput").keyup(function () {
        if (this.value.length == this.maxLength) {
            $(this).next(':input').focus();
        }
    });

          @Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Area, new { @id = "personalDetailMobilePhoneAreaInput",  @class="customText wdt80 numericValue",type = "text",maxlength="2" })
          @Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Area)

          @Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Number, new { @id = "personalDetailMobilePhoneNumberInput",  @class="customText wdt120 mrglft10 phnIE numericValue",type = "text",maxlength="8" })
          @Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Number)

1 个答案:

答案 0 :(得分:0)

试试这个:

$('#personalDetailMobilePhoneAreaInput').keyup(function(){

    var maxLength = $(this).attr('maxlength');
    var currLength = $(this).val().length;        

    if(currLength >= maxLength) 
        $(this).siblings('input').focus();
});

亲自尝试:http://jsfiddle.net/4kjh44Lm/