我遇到了语法问题。如何摆脱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
答案 0 :(得分:1)
我认为您在textarea中显示结果,如果是,请使用\n
代替<br>
<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(""));