简体   繁体   English

如何在获取另一个输入框的值的keyup事件中替换文本区域中的特定文本

[英]How do I replace a specific piece of text in a textarea on a keyup event that gets the value of another input box

How do I replace a specific piece of text in a textarea on a keyup event that gets the value of another input box and replaces the specific piece of text in the textarea? 如何在获取另一个输入框的值并替换文本区域中特定文本的keyup事件上替换文本区域中特定文本的文本?

HTML looks like this - HTML看起来像这样-

    <div>
       <label>test1</label>
       <input name="test_1" id="test1" type="text">

       <label>Message</label>       
       <textarea rows="2" name="textarea_2" id="textarea2">Hello [initial text] blah blah blah</textarea>   
    </div>

I want to replace [initial text] with whatever is put into $('#test1').val(); 我想用$('#test1')。val();中的任何内容替换[初始文本];

Thanks 谢谢

store the string to replace in var: 将要替换的字符串存储在var中:

var lastStr = "[initial text]";

$("#test1").keyup(function() {
    var val = $("textarea").val();
    $("textarea").val(val.replace(lastStr, $("#test1").val()));
    lastStr = $("#test1").val(); // update last str to replace.
});

demo: http://jsfiddle.net/k9uG4/1/ 演示: http : //jsfiddle.net/k9uG4/1/

UPDATE: 更新:

I find the previous example its not safe, Because it replaces all Match char's in Entire area. 我发现前面的示例并不安全,因为它替换了整个区域中的所有Match char。 this example Replaces only the specific piece we want: 本示例仅替换我们想要的特定部分:

var lastStr = "[initial text]";
var startIndex = $("textarea").val().indexOf(lastStr);

$("#test1").keyup(function() {
    var oldVal = $("textarea").val();
    // get all the text before
    var firstPart = oldVal.substring(0, startIndex);
    // get the piece we want And replace it
    var midPart = oldVal.substr(startIndex, lastStr.length).replace(lastStr, $("#test1").val());
    // get all the text after
    var lastPart = oldVal.substring(startIndex + lastStr.length, oldVal.length);

    $("textarea").val( firstPart + midPart +lastPart );
    lastStr = $("#test1").val();
});

demo: http://jsfiddle.net/k9uG4/6/ 演示: http : //jsfiddle.net/k9uG4/6/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在文本框中输入内容来替换文本区域中的JSON文本值 - How to replace JSON text value in the textarea by giving input to the text box jQuery-如何替换键盘输入值? - jQuery - How do I replace the input value on keyup? 在&#39;keyup&#39;事件中获取此输入框的值 - Grab the value of this input box on 'keyup' event 如何将一个输入框中的值传递给另一个输入框 - How Do I pass a value in a input box, to another input box 如何在输入时替换文本框中的用户输入 - How do I replace the user input in text box as it is being typed 如何将多个输入框的值传递给另一个单个输入或文本区域 - How can i pass the value of multiple input box to another single input or textarea 在触发来自代码隐藏的页面加载事件之前,如何在页面加载时将 session 存储值传递到文本框中 - How do I pass a session storage value into a text box on page load before a pageload event from codebehind gets fired 如何访问输入文本框中的值? 我不能在不使用 ref 的情况下直接从事件变量中获取值吗? - How do i access the value inside the input text box? cant i fetch the value directly from the event variable without using ref? 如何通过计算用户在textarea中输入的文本长度来输入特定标签? - How do I input specific tags by calculating the text length which user input in textarea? 具有范围数值的输入框的KeyUp事件的Jquery Regex - Jquery Regex for KeyUp event for an input box with a range numeric value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM