Jquery 如何在下拉列表中使用Json数组值?
我正在尝试在应用程序中创建通知模块。我的qus是如何在Ajax和append下拉列表中使用json数组和for each 这是我的密码-Jquery 如何在下拉列表中使用Json数组值?,jquery,arrays,ajax,json,Jquery,Arrays,Ajax,Json,我正在尝试在应用程序中创建通知模块。我的qus是如何在Ajax和append下拉列表中使用json数组和for each 这是我的密码- <script type="text/javascript"> $( document ).ready(function() { $.ajax({ url: "http://localhost/CodeIgniter_2.2.0/index.php/admin/GetNotificatio
<script type="text/javascript">
$( document ).ready(function() {
$.ajax({
url: "http://localhost/CodeIgniter_2.2.0/index.php/admin/GetNotification",
async: false, //<-- make it synchronous
type: "POST",
dataType: "text",
cache: false,
success: function(response, status, xhr) {
$.each( response, function( key, value ) {
alert( key + ": " + value );
});
}
});
});
</script>
我想在这个下拉列表中使用这个值-
<ul class="dropdown-menu" id="noti" style="background-color:black">
<li>/// here i want all value dynamically with for each ///</li>
</ul>
- ///在这里,我希望所有的值都是动态的///
请帮助..<代码>var数据=[
var data = [
{"tid":"1","message":"Some on ping you","created_date":"2014-11-20 11:15:20.352631"},
{"tid":"2","message":"Hello admin,Aniruddha mishra want to see Rahul patni profile","created_date":"2014-11-20 11:18:21.758673"}
];
#1
var $doc = $('<div />');
$.each(data, function () {
$doc.append($('<li />').text(this.message));
});
$('#noti').html($doc.html());
#2
var menu = $.map(data, function (el) {
return '<li>' + el.message + '</li>';
});
$('#noti').html(menu.join(''));
{“tid”:“1”,“消息”:“关于ping you的一些”,“创建日期”:“2014-11-20 11:15:20.352631”},
{“tid”:“2”,“消息”:“你好,管理员,Aniruddha mishra想看Rahul patni档案”,“创建日期”:“2014-11-20 11:18:21.758673”}
];
#1
变量$doc=$('');
$。每个(数据、函数(){
$doc.append($('').text(this.message));
});
$('#noti').html($doc.html());
#2
变量菜单=$.map(数据、函数(el){
返回“只需使用JQuery append函数。使用ul标签id,附加li标签。希望这能奏效:
$( document ).ready(function() {
$.ajax({
url: "http://localhost/CodeIgniter_2.2.0/index.php/admin/GetNotification",
async: false, //<-- make it synchronous
type: "POST",
dataType: "text",
cache: false,
success: function(response, status, xhr) {
var json=JSON.parse(response);
$.each( json, function( key, value ) {
$("#noti").append('<li id="'+key+'">'+value+'</li>');
});
}
});
});
$(文档).ready(函数(){
$.ajax({
url:“http://localhost/CodeIgniter_2.2.0/index.php/admin/GetNotification",
async:false,成功函数中的//:
success: function(response, status, xhr) {
var html = "";
$.each(response, function(key, value) {
html += "<li>";
html += value.message;
html += "</li>";
//alert( key + ": " + value );
});
$("#noti").append(html);
}
});
成功:功能(响应、状态、xhr){
var html=“”;
$。每个(响应、功能(键、值){
html+=“”;
html+=value.message;
html+=“ ”;
//警报(键+”:“+值);
});
$(“#noti”).append(html);
}
});
试试这个演示:
JS:
var数据=[{
“工业贸易署”:“1”,
“留言”:“有人打你”,
“创建日期”:“2014-11-20 11:15:20.352631”
}, {
“工业贸易署”:“2”,
“消息”:“你好,管理员,Aniruddha mishra想看看Rahul patni的个人资料”,
“创建日期”:“2014-11-20 11:18:21.758673”
}]
对于(i=0;i”);
李艾特({
id:data[i]['tid'],
日期:数据[i][“创建日期”]
}).text(数据[i][“消息]);
$('#noti')。追加(li)
}
成功调用此函数
函数onSuccess(响应)
{
var temp=“”
var noti=document.getelementByID('noti')
如果(响应长度>0)
{
对于(i=0;i
{
temp+=““+response.d[i].消息+” “
}
}
$(“#”+noti).append(临时)
}嘿,我在未捕获的TypeError中发现了这个错误:无法使用'in'运算符搜索'199',因为它是字符串格式的,请转换为json格式,使用json.parse.var json=json.parse(响应);希望它现在能工作。
success: function(response, status, xhr) {
var html = "";
$.each(response, function(key, value) {
html += "<li>";
html += value.message;
html += "</li>";
//alert( key + ": " + value );
});
$("#noti").append(html);
}
});
var data = [{
"tid": "1",
"message": "Some on ping you",
"created_date": "2014-11-20 11:15:20.352631"
}, {
"tid": "2",
"message": "Hello admin,Aniruddha mishra want to see Rahul patni profile",
"created_date": "2014-11-20 11:18:21.758673"
}]
for (i = 0; i < data.length; i++) {
var li = $('<li></li>');
li.attr({
id: data[i]['tid'],
date: data[i]['created_date']
}).text(data[i]['message']);
$('#noti').append(li)
}