[英]How to display json data from js file in html page inside div
在这里,我正在将数据从JSON格式的db中获取到我的.js文件中,也正在div中。但是我没有在HTML页面中获取div。请任何人告诉我如何在HTML页面中显示。是我的代码:
我的json数组:
[{"chat_question_id":"1",
"chat_question_title":"What is PHP?"},
{"chat_question_id":"17",
"chat_question_title":"what is php?",}
以下是我的js代码:
function ChatQuestionsInfo(bRowId)
{
var actionType = "";
var hdnFlagForSearchQue = $("#hdnFlagForSearchQue").val();
if(hdnFlagForSearchQue=="insert"){
actionType = "ChatQuestionsInfo";
} else {
actionType = "searchQuestiontitle";
}
if($.trim($("#questionname").val())==""){
$("#questionname").focus();
alert("Enter Question Name");
return false;
}
if($.trim($("#technologytags").val())==""){
$("#technologytags").focus();
return false;
}
$.post(rootUrl+"includes/ajax/ajax_chat.php", {action: actionType,bRowId:bRowId,bQuestionName: $.trim($("#questionname").val()),bTechnologyTags: $.trim($("#technologytags").val())},
function(data){
var htmlText = '';
for ( var key in data ) {
htmlText += '<div class="tab-content">';
htmlText += '<div id="newquestions"> : ' + data[key].chat_question_title + '</div>';
htmlText += '</div>';
}
$('.chat_body_form').append(htmlText);
}, "json");
return false;
}
HTML代码:
<div id="newquestions"></div>
我认为您需要进行2处更改,其中1处在ajax_chat.php文件中。 其次是您的javascript。
例如
header('Content-Type:application/json');
echo json_encode('your varialbe array');
例如
function(data){
$.each(data,function(i){
$('.chat_body_form').append('<div class="tab-content"><div id="newquestions"> : '+ $(this).chat_question_title + '</div></div>';
});
}
我认为这些更改将起作用,并且将成为理解或更改非常非常简单的代码,
谢谢...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.