2014-11-15 17 views
5

Mam formularz HTML i po naciśnięciu enter wybierane jest następne pole.
tutaj jest kod.Wybór następnego pola przy naciśnięciu enter przy użyciu jquery

$(document).on("keydown","input",function(event) { 
 
    \t if (event.which === 13) { 
 
    \t \t event.stopPropagation(); 
 
    \t \t event.preventDefault(); 
 
    \t \t $(this).nextAll("input").eq(0).focus(); 
 
    \t } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    \t <table> 
 
    \t \t <tr> 
 
    \t \t \t <td>Medical Record No.</td><td><input type="text" name="labNo" /></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Age/sex </td><td><input type="text" name="age" /> 
 
    \t \t \t \t <select id="age"> 
 
    \t \t \t \t \t <option value="Year">Year</option> 
 
    \t \t \t \t \t <option value="Month">Month</option> 
 
    \t \t \t \t </select> 
 
    \t \t \t \t <select id="sex"> 
 
    \t \t \t \t \t <option value="Male">Male</option> 
 
    \t \t \t \t \t <option value="Female">Female</option> 
 
    \t \t \t \t </select> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Phone </td> 
 
    \t \t \t <td><input type="text" name="" value="-,-" /></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td colspan="2"><input type="button" id="patBtn" value="Save (S)" accesskey="s" /> 
 
    \t \t \t \t <input type="reset" value="Set Default (D)" accesskey="d" /> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t </table> \t 
 
    </form>

To nie działa.

Odpowiedz

0

to powinno działać bez względu na strukturę HTML

$(document).on("keydown","input",function(event) { 
if (event.which === 13 || event.keyCode === 13) { 
    event.stopPropagation(); 
    event.preventDefault(); 
    var position = $(this).index('input'); 
    $("input").eq(position+1).focus(); 
} 
}); 

również Wybierz:

$(document).on("keydown, keyup","input, select",function(event) { 
if (event.which === 13 || event.keyCode == 13) { 
    event.stopPropagation(); 
    event.preventDefault(); 
    var position = $(this).index('input, select'); 
    $("input, select").eq(position+1).focus(); 
} 
}); 

jsfiddle: http://jsfiddle.net/xh0m7pzu/1/

+0

jak to zrobić w opcji 'wybierz' również. @A_funs – Axeem

+0

działa w firefoxie i safari –

+0

właściwie to zmieniłem, dodałem też keyup, jak się wydaje działa we wszystkich przeglądarkach teraz –

1

Spróbuj Poniższy skrypt

$(document).on("keydown","input",function(event) { 
    debugger; 
    if (event.which === 13) { 
     event.stopPropagation(); 
     event.preventDefault(); 
     $(this).parents("tr").next().find("input").focus(); 
    } 
}); 

Demo

Powiązane problemy