Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 退格正则表达式返回到输入正则表达式数组值的末尾_Javascript_Jquery_Arrays_Regex - Fatal编程技术网

Javascript 退格正则表达式返回到输入正则表达式数组值的末尾

Javascript 退格正则表达式返回到输入正则表达式数组值的末尾,javascript,jquery,arrays,regex,Javascript,Jquery,Arrays,Regex,我有一个输入,这个输入限制为10。如果用户在数字数组的中间删除或退一个数字,则光标或退格线现在位于数组的末尾,而不是用户首先选择要放置的退格空间的位置。我希望这样,当用户选择这个数字数组,并想删除中间的两个数字时,他们可以这样做,而不需要退格游标跳转到数字数组的末尾。 HTML 只需记住位置,然后在完成操作后更新位置 var y=函数(e){ var目标=e.target; varx=e.target.value.replace(/\D/g',).match(/(\D{0,3})(\D{0,3

我有一个输入,这个输入限制为10。如果用户在数字数组的中间删除或退一个数字,则光标或退格线现在位于数组的末尾,而不是用户首先选择要放置的退格空间的位置。我希望这样,当用户选择这个数字数组,并想删除中间的两个数字时,他们可以这样做,而不需要退格游标跳转到数字数组的末尾。 HTML


只需记住位置,然后在完成操作后更新位置

var y=函数(e){
var目标=e.target;
varx=e.target.value.replace(/\D/g',).match(/(\D{0,3})(\D{0,3})(\D{0,4})/);
var值=!x[2]?x[1]:x[1]+x[2]+(x[3]?x[3]:“”);
如果(值===e.target.value){
返回;
}
变量位置=target.selectionStart;
e、 target.value=value;
target.selectionEnd=position;//将光标设置在所需位置。
};
document.getElementById('test')。addEventListener('input',y);
//为了避免按下非数字键(如a-z)时出现混乱行为
document.getElementById('test')。addEventListener('keypress',函数(e){
如果(e.哪个<48 | | e.哪个>57){
e、 预防默认值();
}
});
<div>
<input type="text" id="test" name="test" style="width: 210px;" maxlength="10" />
</div>
var y =  function (e) {
            var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
            e.target.value = !x[2] ? x[1] : x[1] + x[2] + (x[3] ? x[3] : '');
            var last = x[3];
            };    
            document.getElementById('test').addEventListener('input', y);
var y = function(e) {    
  var target = e.target;    
  var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
  var value = !x[2] ? x[1] : x[1] + x[2] + (x[3] ? x[3] : '');

  if (value === e.target.value) {
    return;
  }

  var position = target.selectionStart;
  e.target.value = value;      
  target.selectionEnd = position; // set the cursor on desired position.
};

document.getElementById('test').addEventListener('input', y);

//In order to avoid jumb behaviour when press non-numeric like a-z
document.getElementById('test').addEventListener('keypress', function(e) {
  if (e.which < 48 || e.which > 57) {
    e.preventDefault();
  }
});