![](/img/trans.png)
[英]How to move cursor/caret to last character in input field when content is larger than input field (in Chrome)?
[英]use jQuery to move the cursor to another field when a certain character is typed?
我的网页上有两个文本字段。 用户应输入由连字符(“ - ”)字符分隔的两个数字。 数字可以是1到10位数。 当用户按下连字键时,我需要光标移动到下一个字段。
我可以使用$('#txtField2').focus()
轻松移动光标。 但是,我仍然遇到连字符仍然在第一个文本字段中的问题。 如何轻松地抑制连字符出现在第一个文本字段中?
<form>
<input type='text' class='num' />
<input type='text' class='num' />
</form>
$('.num:first').keydown(function (event) {
// check for hyphen
if (event.which === 189) {
event.preventDefault();
$(this).next('.num').focus();
}
});
假设一个简化的html:
<form action="#" method="post">
<fieldset>
<label for="numOne">Number:</label>
<input type="text" id="numOne" name="numOne" />
<input type="text" id="numTwo" name="numTwo" />
</fieldset>
</form>
以下内容应该有效,或作为示例:
$('#numOne').keypress(
function(e){
if (e.which == 45) {
$(this).next('input:text').focus();
return false; // prevents the '-' being entered.
}
});
顺便说一下,我使用$(this).next('input:text')
而不是基于id的选择器来允许更一般的应用和重用。
参考文献:
keypress()
, next()
。 我这样做:
$('#input1').keypress(function(e) {
if(e.keyCode == 45) {
e.preventDefault();
$('#input2').focus();
}
});
看这里的实例: http : //jsfiddle.net/wjNP3/2/
你可以“听”那个角色,只关注另一个角色并防止它输入
也许这可以帮助你:
HTML
<div>
<p>Insert Your Number:</p>
<input type="number" value="" id="first" >
<input type="number" value="" id="second" >
<input type="number" value="" id="third" >
<input type="submit" id="submit">
</div>
jQuery的
$("#first").on("keypress", function(){
if($("#first").val().length == 4){
$("#second").focus();
}
})
$("#second").on("keypress", function(){
if($("#second").val().length == 4){
$("#third").focus();
}
})
$("#third").on("keypress", function(){
if($("#third").val().length == 5){
$("#submit").focus();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.