1.Jquery每个函数(键,值(结果只返回最后一个键和值。2.它在警报框中运行良好3.在将数据转换为HTML时,它只返回最后一个键和值
var result = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","Phone":"123 dead drive"}';
$.each($.parseJSON(result), function(k, v) {
console.log(k + ' is ' + v);
$('#stage').html('<p>' + k +':' + v+ '</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="stage" style = "background-color:#cc0;">
STAGE
</div>
我不知道如何获得HTML中的所有值,请提前帮助,谢谢。
您也可以使用.append()
而不是.html()
var result = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","Phone":"123 dead drive"}';
$.each($.parseJSON(result), function(k, v) {
//console.log(k + ' is ' + v);
$('#stage').append('<p>' + k + ':' + v + '</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="stage" style="background-color:#cc0;">
STAGE
</div>
因为在每个循环中都要重写#stage
上的html。您需要将内容存储在变量中,循环结束后,将resutl插入文档中。
var result = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","Phone":"123 dead drive"}';
var html = "";
$.each($.parseJSON(result), function(k, v) {
html += '<p>' + k +':' + v+ '</p>';
});
$('#stage').html(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="stage" style = "background-color:#cc0;">
STAGE
</div>