如何从JSON数组中获取数据并放入listview?

时间:2012-07-16 07:19:47

标签: javascript jquery-mobile

您好我是javascript编程新手,我在jquery mobile中创建了一个下拉列表视图(选择器视图),因为我在该选择器视图中创建了一个json数组。问题是我需要代码,当我点击一个特定的列表视图时,它应该运行一个js函数,该函数从json数组中获取数据并将其放入picker(list)视图中。 我的HTML代码是:

<form method="get" name="datarange">
  <div data-role="fieldcontain">
     <select id="number" name="day you need" onclick="dayLoad();">
       <option value="select-value" selected="selected" >--Select Day--</option>
       <option value="01">/* here i want my data from array "day"/*</option>
       <option value="02">/* here i want my data from array "day"/*</option>
     </select>
  </div>
  <div data-role="fieldcontain">
     <select id="time" name="time you need">
       <option value="select-value" selected="selected">--Select Time--</option>
       <option value="101">/* here i want my data from array "Time" /*</option>
       <option value="102">/* here i want my data from array "Time"/*</option>
       <option value="103">/* here i want my data from array "Time"/*</option>
       <!--  etc. -->
       <option value="116">/* here i want my data from array "Time"/*</option>
     </select>
  </div>
  <div data-role="fieldcontain">
     <select id="pid" name="pid you need">
       <option value="select-value" selected="selected">--No PID allocation found--</option>
       <option value="301">/* here i want my data from array "pid"/*</option>
       <option value="302">/* here i want my data from array "pid"/*</option>
     </select>
  </div>
  <div data-role="fieldcontain">
     <select id="jus" name="justification you need">
       <option value="select-value" selected="selected">--Select Justification--</option>
       <option value="201">Project Work</option>
       <option value="202">Client Call</option>
       <option value="203">Team Meeting<option>
       <option value="204">Others</option>
     </select>
  </div>
</form>
<div id="display" class="rss-box"></div>

在我的js文件中:

var day=[
       {"dayTime" : "Today-Drop"},
       {"dayTime" : "Tomorrow-Pick up"}];  
var time=[
        {"PM" : "3:00 PM"},
        {"PM" : "4:00 PM"},
        {"PM" : "7:30 PM"},
        {"PM" : "9:00 PM"},
        {"PM" : "10:15 PM"},
        {"PM" : "11:00 PM"}];

var Pid=[
        {"pidno" : "7813"},
        {"pidno" : "8133"},];


function dayLoad(){//i need the code to put in this function which grabs the data from array and gonna put into the listview}

3 个答案:

答案 0 :(得分:0)

如果按照其他一些答案的解释正确插入,问题可能仍然是jquery mobile没有修改dom,以便根据jquery移动样式表进行渲染。这是因为jquery mobile仅在加载页面时才进行这种转换。

然而,jquery mobile确实提供了一种方法来修改已通过javascript函数插入到dom中的内容。此方法是触发器('create')。您需要选择已使用$插入dom的元素,然后按以下方式调用trigger:

$("css selector for the element").trigger('create')

应用于日期保管箱,可以这样做:

$("#number").trigger('create')

答案 1 :(得分:0)

类似的东西:

function appendNodes( target, data, key ) {
    var div = document.getElementById(target);
    for (var i = 0; i < data.length; i++ ) {
        var option = document.createElement("option");
        option.appendChild( document.createTextNode(data[i][key]) )
        option.value = i;
        div.appendChild(option);
    }
}

function dayLoad(){
    appendNodes( "number", day, "dayTime" );
    appendNodes( "time", time, "PM" );
    appendNodes( "pid", Pid, "pidno" );
}

答案 2 :(得分:0)

只需迭代您的数据,json(JavaScript Object Notation)就是javascript:

function dayLoad() {
    for (var key in day) {
        $('#number').append($("<option/>", {
            value: key,
            text: day[key].dayTime
        }));
    }
}

或jQuery流畅的风格:

function dayLoad() {    
    $.each(day, function (key, value) {
        $('#number')
            .append($("<option></option>")
            .attr("value", key)
            .text(value.dayTime));
    });
}

我不明白你为什么要使用onclick事件,也许你应该看看jquery ready method。 初始化可能如下所示:

$(document).ready(function () {        
            dayLoad();        
    });

[更新请参阅@Calavoow评论] 移动使用$(document).bind('pageinit'),而非$(document).ready()