输出数据时如何摆脱html标签

时间:2017-10-15 05:20:48

标签: jquery ajax

我遇到了语法问题。如何摆脱jQuery中的html标签?

$.ajax({
        type: "POST",
        url: '/populate',
        data: {id: id, '_token':$('input[name=_token]').val()},
        success: function( data ) {
          var results = [
              data.address,
              data.state,
              data.city,
              data.country,
              data.code
          ];

          $("#title").val(data.title);
          $("#address").val(results.map(function(value) {
               return(value + "<br>";
            }).join(""));

        }
    });

显示结果时显示html标签。请问如何解决?

9111 Parisian Mountain
Cletamouth, MO 99096-0914<br>Pennsylvania<br>West 
Khalilborough<br>Uzbekistan<br>23457-3306

2 个答案:

答案 0 :(得分:1)

我认为您在textarea中显示结果,如果是,请使用\n代替<br>

如果您尝试在html代码中插入换行符,

<br>会有效,但由于address是新换行符的输入元素,您应该使用\n

var results = ["9111 Parisian Mountain Cletamouth, MO 99096-0914","Pennsylvania","West Khalilborough","Uzbekistan","23457-3306"]; 

$("#address").val(results.map(function(value) {
       return(value + "\n");
  }).join(""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="address" cols="55" rows="5"></textarea>

答案 1 :(得分:0)

使用.html()代替.val()用于HTML

  $("#address").html(results.map(function(value) {
       return(value + "<br>");
  }).join(""));

http://api.jquery.com/html/#html-htmlString