jQuery String Replace in TextArea

前端 未结 4 757
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 22:39

I\'m trying to do a string replace in a textarea after the user has entered their content and nothing I\'ve tried is working. Any help would be greatly appreciated. This is wher

相关标签:
4条回答
  • 2021-01-23 23:03

    Your code works, you just need to use jQuery instead of Mootools in your jsfiddle, probably also just use the change event so that it replaces the text only after the textarea has been updated

    0 讨论(0)
  • 2021-01-23 23:07

    Try this,

    $("#field_id_29").change(function(){
        var text = $(this).val();
        var regexp = /www-source/gi;
        if ( text.match(regexp) ){      
            text = text.replace(/www-source/g,"www");
            return $(this).val(text);
        }
        return false;
    });
    

    When the field changes we grab the value, compare if against a regular expression and if we have a match we replace the text and we are done, otherwise we just return.

    0 讨论(0)
  • 2021-01-23 23:09

    Try this,

    Live Demo

    [$("#field_id_29").bind("keyup", function() {
        var text = $(this).val();
        text = text.replace(/source/g, "www")
        $(this).val(text);
    });​
    
    0 讨论(0)
  • 2021-01-23 23:13

    jsfiddle

            $("#field_id_29").bind("change keyup input",function(){    
                this.value= this.value.replace(/www-source/g,"www");                 
            });
    ​
    

    Note: inside function this is reference of textarea so you directly use this.value instead of accessing textarea value using id.

    0 讨论(0)
提交回复
热议问题