how do you strip html tags in textarea input

后端 未结 6 1662
自闭症患者
自闭症患者 2020-12-16 21:00

I have set up a little web application where you add post-its to a page. It\'s just a project to help better learn JavaScript/jQuery and it seemed like an appropriate projec

相关标签:
6条回答
  • 2020-12-16 21:32
    $("textarea.text_input").on('keyup', function() {
        this.value = this.value.replace(/<(.|\n)*?>/g, '');
    });
    

    FIDDLE

    0 讨论(0)
  • 2020-12-16 21:36

    No one successfully answered this question in accordance with the OP's request, in that the inner tags would be completely removed from the input, not just the tags themselves; so, here's an answer that will do precisely that (Fiddle):

    var input = "This is <b>not</b>a successful test";
    var output = $("<div/>").html(input).children().remove().end().text()
    
    0 讨论(0)
  • 2020-12-16 21:40
    var textAreaValue = "text <br /> more text";
    var strippedText = $("<div/>").html(textAreaValue).text();​
    

    And here's a Fiddle.

    0 讨论(0)
  • 2020-12-16 21:44
    function stripHTML(text){
       var regex = /(<([^>]+)>)/ig;
       return text.replace(regex, "");
    }
    // USE: var str = stripHTML('<b>test</b>');
    

    Demo: http://jsfiddle.net/pisabev/eJzfw/3/

    0 讨论(0)
  • 2020-12-16 21:44

    You could use the jquery .text function, though it will rather escape the html than strip it:

    $(".myTxtBox").text("<b>this text will be displayed, even the b tag.");
    
    0 讨论(0)
  • 2020-12-16 21:49

    You have three issues: One is that the method is not returning properly (that's the fault of whoever wrote it). Next is that you are defining the method within $(document).ready(), so it is not able to be found (at least in Chrome). Third, you are not calling it the right way (onSubmit will never be used here).

    I've used @Joao's method (so you should upvote his answer) and used this method, moved outside of the ready() function:

    function stripHTML(str){
      var strippedText = $("<div/>").html(str).text();
      return strippedText;
    }​
    

    And also called it here:

    var post = stripHTML($(".text_input").val())
    

    Here is the working fiddle: http://jsfiddle.net/ee3MH/

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