[英]How do i replace <input> letters with other letters onclick?
我希望能夠在框中輸入單詞或名稱,單擊生成按鈕,然后將某些字母或單詞更改為新框。
例如:John Smith> Juhn Smith(將Juhn Smith的“ o”更改為“ u”)或 約翰·史密斯(John Smith)>羅恩·史密斯(Ron Smith)(更改整個單詞)
我曾嘗試查看字符串和replacewith()等,但一直在使用輸入框來查找任何合適的東西,尤其是沒有任何東西。
這是一個接近我的意思的示例,但更加復雜:
謝謝
@James當尋求幫助時,下次嘗試自己做。 這是一個顯示您的示例的HTML頁面。 將此另存為myPage.html。 然后在瀏覽器中打開文件。
<!DOCTYPE html>
<html>
<meta charset="ISO-8859-1">
<head>
<title>Replace web page</title>
</head>
<script type="text/javascript" >
// this gets called by pressing the button
function myFunction() {
var first = document.getElementById("foreName");
var last = document.getElementById("lastName");
// now change 'o' to 'u' for each name part
var changedFirst = myChange(first.value);
var changedLast = myChange(last.value);
// now move it to another element
var resultBox = document.getElementById("resultBox");
resultBox.innerHTML = "" + changedFirst + " " + changedLast;
}
// this is a function to search a string and replace with a substitute
function myChange(str) {
var arr = str.split('');
var result = "";
for(var i=0; i < arr.length; i++) {
if (arr[i] == 'o') // if it's an o
result += 'u'; // replace it with 'u'
else
result += arr[i];
}
return(result);
}
</script>
<body>
<p> This is an example of inputting text, changing it, then displaying it into another item</p>
First Name: <input type="text" name="foreName" id="foreName" maxlength=100 value="">
Last Name: <input type="text" name="lastName" id="lastName" maxlength=100 value="">
<p>
<input type="button" id="inButton" name="inButton" value="Click Me" onclick="myFunction()" >
</p>
<p>
<textarea rows="1" cols="50" id="resultBox"></textarea>
</p>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.