可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i have a multiline textfield("Adressfeld"), and i want to Uppercase every first letter and LowerCase the rest of every single word in this text area.
Here is my try:
function capitalize(Eingabe){
Eingabe = this.getField("Adressfeld").value;
var strArr = Eingabe.split(" ");
var newArr = [];
for(var i = 0 ; i < strArr.length ; i++ ){
var FirstLetter = strArr[i].charAt(0).toUpperCase();
var restOfWord = strArr[i].slice(1).toLowerCAse();
newArr[i] = FirstLetter + restOfWord;
}
return newArr.join(' ');
}
Ausgabe = this.getField("Empfängername");
Ausgabe.value = capitalize();
With the script shown above, every single word in the first line of the text area is capitalized. But in every other line, the first word isn't capitalized.
How i have to change the script to get it work?
Thanks,
回答1:
I have included an example below try like that, it will solve your problem
Html
<input type="button" value="clk" onclick="z();"/>
<textarea rows="4" id="text" cols="50">
JS
function z()
{
var z=document.getElementById("text").value;
var x=z.replace(/\b./g, function(m){ return m.toUpperCase(); });
alert(x);
}
DEMO
I you want to Convert Every first letter of each word to upper and all other letters are lower then first convert the entire string to lowercase.Then do the same things as above.
DEMO2
回答2:
Meinst du sowas?
var textToArray = document.getElementById('myTextarea').value.split('\n');
/* Hier eine Schleife zur Bearbeitung von textToArray */
var arrayToString = textToArray.join(' ');
回答3:
The split
operation fails -- the result of the first split
cannot be split again on another character.
I see no reason to first replace returns by a single type \n
and then try to split on either \n
or a space. It's way easier to just replace the returns by a single space and only then split:
var strArr = String(Eingabe.value).replace(/[\r\n]+/g," ").split(" ");
With that, the rest seems to work.
Here is another approach, which may or may not work as well (it depends on whether Javascript's interpretation of "word boundary" \b
agrees with your own):
function capitalize(Eingabe){
// Eingabe = this.getField("Adressfeld");
var strArr = String(Eingabe.value).replace(/[\r\n ]+/g,' ');
strArr = strArr.replace (/\b[a-z]/g, function(found) { return found.toUpperCase(); });
return strArr;
}
回答4:
A few things:
• The argument of the function is Eingabe. In this case, this is a variable containing a value, and it does not make sense at all to redefine in the first line of the function. The way the function looks like, you won't need an argument, and therefore, your function definition looks like this:
function capitalize() {
• That done, define Eingabe properly as var Eingabe
.
• With the array, you essentially want to create a two-dimensional array. First create the array of lines and then a loop through the lines, as it has been suggested in the answer by @Entimon
• You end with
this.getField("Adressfeld").value = capitalize() ;
And that should do it.
回答5:
thanks for your help!
The correct answer is based on Arunprasanth KV's jsfiddle.net/svh1jd99
function capitalize()
{
var capitalize = this.getField("Adressfeld").value;
var done = capitalize.replace(/\b./g, function(m){ return m.toUpperCase();});
return done;
};
this.getField("Adressfeld").value = capitalize();
Thanks again for your help.
回答6:
By using jQuery you can do this as shown below:
Demo: https://jsfiddle.net/cxow8198/3/
<input type="text" id="input">
<script>
//usage
$("input").keyup(function() {
toUpper(this);
});
//function
function toUpper(obj) {
var mystring = obj.value;
var sp = mystring.split(' ');
var wl=0;
var f ,r;
var word = new Array();
for (i = 0 ; i < sp.length ; i ++ ) {
f = sp[i].substring(0,1).toUpperCase();
r = sp[i].substring(1).toLowerCase();
word[i] = f+r;
}
newstring = word.join(' ');
obj.value = newstring;
return true;
}
</script>
Query code snippet to make capitals of the first letter of every word in the string. This could be used to prevent users from entering all caps for titles or text when they input data into forms. Hope this helps...