Javascript Angularjs:禁用选项卡键默认行为
我正在开发一个输入表。Javascript Angularjs:禁用选项卡键默认行为,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我正在开发一个输入表。 我想要的是:当use按下“+”键时(光标在表格行中的任何位置),应用程序会在表格中添加一行新内容。 这样做效果很好: <tr ng-repeat="sell in sells" ng-keypress="newLine($event)"> 我的问题是,当用户在该行的一个输入中按下tab键转到下一个输入时,下一个输入值将突出显示(这是tab键的正常行为)。 然后,如果用户按“+”添加新行,它将用“+”符号替换输入值 我已经设置了一个指令,允许用户只在输入
我想要的是:当use按下“+”键时(光标在表格行中的任何位置),应用程序会在表格中添加一行新内容。
这样做效果很好:
<tr ng-repeat="sell in sells" ng-keypress="newLine($event)">
我的问题是,当用户在该行的一个输入中按下tab键转到下一个输入时,下一个输入值将突出显示(这是tab键的正常行为)。然后,如果用户按“+”添加新行,它将用“+”符号替换输入值 我已经设置了一个指令,允许用户只在输入中键入数字,但当突出显示输入值时,它不起作用
angular.module('myApp').directive('numbersOnly', function($timeout) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/[^0-9.]+/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
<td style="width:59px"><input type="text" ng-model="sell.quantity" numbers-only enter-as-tab ></td>
angular.module('myApp')。指令('numbersOnly',函数($timeout){
返回{
要求:'ngModel',
链接:函数(范围、元素、属性、模型控制){
modelCtrl.$parsers.push(函数(inputValue){
//如果在输入上使用ng required,则下一步是必要的。
//在这种情况下,当首先键入字母时,将调用此解析器
//同样,第二次,该值将是未定义的
如果(inputValue==未定义)返回“”
var transformedInput=inputValue.replace(/[^0-9.]+/g');
if(transformedInput!=输入值){
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
返回转换输入;
});
}
};
});
如果有人知道防止用户用“+”替换突出显示的值的方法。。。。或禁用tab键的默认行为
提前感谢。使用表单验证甚至
ng更改处理程序可能会更好,但是如果您喜欢使用现有的$parser
方法,可以使用$modelValue
还原为原始值:
angular.module('myapp',[])指令('numbersOnly',函数($timeout){
返回{
要求:'ngModel',
链接:函数(范围、元素、属性、模型控制){
modelCtrl.$parsers.push(函数(inputValue){
if(inputValue==未定义)返回“”;
var transformedInput=inputValue.replace(/[^0-9.]+/g');
if(transformedInput!=输入值){
modelCtrl.$setViewValue(modelCtrl.$modelValue);
modelCtrl.$render();
if(inputValue.indexOf(+')>-1){
警告(“可能在此处触发您的换行符”);
}
返回modelCtrl.$modelValue;
}否则{
返回输入值;
}
});
}
};
});代码>
您可以使用自定义指令覆盖“+”键的默认操作
module.directive('overridePlusKey', ['$window', function ($window) {
// Linker function
return function (scope, element, attrs) {
element.bind('keydown', function (e) {
var keyCode = e.keyCode || e.which;
console.log(keyCode);
if (keyCode == 107 || keyCode == 187)
{
e.preventDefault();
// Your code here to add a row
}
});
};
}]);
然后将其应用于输入,如下所示:
<input type="text" ng-model="content" override-plus-key />
您不能使用
而不是var transformedInput=inputValue。替换(/[^0-9.]+/g')代码>?类型编号的问题是相同的,如果该值突出显示,按“+”将在输入中输入一个空值