2
我在這裏做錯了什麼?JQuery在輸入文本中查找和替換textarea中的字符
我的HTML
<form name="frm">
<textarea rows="10" id="uinput">some text here</textarea>
find 1: <input type="text" id="findbox1" value="e">
replace 1: <input type="text" id="replacebox1" value="666">
find 2: <input type="text" id="findbox2" value="o">
replace 2: <input type="text" id="replacebox2" value="000">
find 3: <input type="text" id="findbox3" value="t">
replace 3: <input type="text" id="replacebox3" value="777">
<button type="button" id="fnrbtn">find and replace</button>
</form>
我的JQuery
RegExp.escape = function(str) {
return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
};
$("#fnrbtn").click(function() {
var InputBox = $("#uInput").val(),
FindWhat = $('#findbox' + i).val(),
ReplaceWhat = $('#replacebox' + i).val(),
NewInputBox = '',
msg = '';
for (i = 1; i < counter; i++) {
msg += "\n Textbox #" + i + ": " + $('#findbox' + i).val() + " Replace #" + i + ": " + $('#replacebox' + i).val();
NewInputBox = InputBox.replace(new RegExp(RegExp.escape(FindWhat), "gi"), ReplaceWhat);
}
alert(msg);
$("#uInput").val(NewInputBox);
$('#uInput').select();
});
這是否幫助?只是好奇,因爲我剛剛注意到你刪除的答案。 –