2017-02-22 148 views
1

我想在我的textarea中大写第一个字母和第三个字母。如何大写第一个和第三个字母?

我只能大写首字母,然后每个下一个字母和单词都转换为小写。

如果这个问题有任何解决方法,请告诉我。

我正在使用AngularJS。

这就是即时尝试和做的。

link: function (scope, iElement, iAttrs, controller) { 
     //console.log('init'); 

     controller.$parsers.push(function (inputValue) { 
      var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase() + inputValue.substr(1).toLowerCase() : ''; 
         if (transformedInput != inputValue) { 
      controller.$setViewValue(transformedInput); 
      controller.$render(); 
     } 

     return transformedInput; 
    }); 

这只适用于第一个字母,它会转换为大写,然后将另一个字母和单词转换为小写。

我试图改变我的代码,但没有。

var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase() + inputValue.substr(1).toLowerCase() + inputValue.charAt(3).toUpperCase() + inputValue.substr(4).toLowerCase(): ''; 

回答

2

看看这个。与您正在使用for循环来标识要修改的字符索引相同。

var inputValue = "test"; 
 

 
var transformedInput = ''; 
 

 
if(inputValue){ 
 
    for(var i=0; i<inputValue.length; i++){ 
 
    \t if(i===0 || i=== 2){ 
 
\t  transformedInput += inputValue.charAt(i).toUpperCase(); 
 
     } else { 
 
    \t  transformedInput += inputValue.charAt(i).toLowerCase(); 
 
    } 
 
    } 
 
} 
 

 
console.log(transformedInput);

+0

,它工作正常!非常感谢 – brithwulf

1

这是在特定的位置,以利用字符

function capitalizeAtPositions(string, indexes) { 
    (indexes || []).forEach(function(index) { 
    if (string.length < index) return; 

    string = string.slice(0, index) + 
     string.charAt(index).toUpperCase() + string.slice(index+1); 
    }); 
    return string; 
} 

运行它如下功能:

var test = "abcdefg"; 
var result = capitalizeAtPositions(test, [0, 2]); 
//AbCdefg 

你的情况,我认为这将是这样的(不能测试它没有jsfiddle):

var transformedInput = capitalizeAtPositions(inputValue || '', [0, 2]); 
+0

我能不能把这个代码替换成我的?我需要它输入的文本不是文本。当我输入一些东西时,应该首字母大写,例如第三个字母。我认为这只适用于不输入文字 – brithwulf

+0

@brithwulf是的,你可以。如果您在输入时需要更改文本,则无法轻松完成。你将不得不使用'div contenteditable'或者一些更高级的解决方案,比如'CodeMirror',我想。感谢的人, – euvl

0

试试这个

var firstUpperCase = inputValue.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); 

    var transformedInput = ''; 
     for(i = 0; i < firstUpperCase.length; i++){ 
      if(i > 0 && i % 3 == 0){ 
       transformedInput += firstUpperCase[i].toUpperCase(); 
      }else{ 
       transformedInput += firstUpperCase[i]; 
      } 
     } 
1

看到你如何需要有输入改变输入文字时,你可能需要一个指令;这里有一个大写的任何输入的给定的字母与NG-模型:

https://plnkr.co/edit/hWhmjQWdrghvsL20l3DE?p=preview

app.directive('myUppercase', function() { 
    return { 
    scope: { 
     positions: '=myUppercase' 
    }, 
    require: 'ngModel', 
    link: function(scope, elem, attrs, ngModelCtrl) { 

     scope.positions = scope.positions || [] 

     function makeString(string) { 
     if (!string) return; 
     angular.forEach(scope.positions, function(pos) { 
      string = string.slice(0, pos) + string.slice(pos, pos+1).toUpperCase() + string.slice(pos + 1) 
      console.log(string) 
     }) 
     return string; 
     } 

     ngModelCtrl.$parsers.push(makeString) 
     ngModelCtrl.$formatters.push(makeString) 
    } 
    } 
}) 

HTML:

<input ng-model="value" my-uppercase="[0, 2]"> 
1

我简单的解决方案

var inputValue = 'your value'; 

function toUpper (str) { 
    var result = ''; 
    for (var i = 0; i < str.length; i++) { 
     if (i === 0 || i === 2) { 
      result += str[i].toUpperCase(); 
     } else { 
      result += str[i].toLowerCase(); 
     } 
    } 
    return result; 
} 
var transformedInput = toUpper(inputValue); 
相关问题