1

嗨,我是 javascript 编程的新手,我正在 jquery mobile 中制作一个下拉列表视图(选择器视图),以便数据位于该选择器视图中,我创建了一个 json 数组。问题是我需要代码,当我单击特定列表视图时,它应该运行一个 js 函数,该函数从 json 数组中获取数据并将其放入选择器(列表)视图中。我的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}
4

3 回答 3

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()

于 2012-07-16T08:06:10.400 回答
0

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

但是 jquery mobile 确实提供了一种方法来修改通过 javascript 函数插入到 dom 中的内容。这个方法是trigger('create')。需要用 $ 选择已经插入到 dom 中的元素,然后按如下方式调用 trigger:

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

应用到 day Dropbox 这将是这样完成的:

$("#number").trigger('create')
于 2012-07-16T07:32:26.237 回答
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" );
}
于 2012-07-16T07:37:54.043 回答