2017-02-22 147 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); 
相關問題