如何通过函数调用动态传递变量?

时间:2014-05-02 18:04:52

标签: jquery

我想传递变量 ' detailVar.titel',' detailVar.location',' detailVar.datum',' detailVar.datumEnde' 至 ' title',' location',' startDate',' endDate',

我正在使用:https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin/tree/pre-3.0,jquery-1.11.0,jquery.mobile-1.3.2和phonegap 2.9。

非常感谢任何帮助!

    $('#detailPage').live('pageshow', function(event) {
        var id = getUrlVars()["id"];
        $.getJSON(serviceURL + 'getDetail.php?id='+id, displayDetail);
    });

    function displayDetail(data) {
        var detailVar = data.item;
        console.log(detailVar);

    // title            
        $('#detailTitel').text(detailVar.titel);
    //location      
        $('#detailLocation').text(detailVar.location);
    // date     
        $('#detailDatum').text(detailVar.datum);    
    // end date     
        $('#detailDatumEnde').text(detailVar.datumEnde);    

    //  Calendar
     // prep some variables
     function createMyEvent(){

          var startDate = new Date("May 01, 2014 13:00:00");    //    location(detailVar.datum); is not working
          var endDate = new Date("May 01, 2014 14:00:00");  //    location(detailVar.datumEnde); is not working
          var title = "myTitle";                                                        //    location(detailVar.titel); is not working
          var location = "myLocation";                                          //    location(detailVar.location); is not working
          var notes = "myNotes";
          var success = function(message) { alert("yes!"); };
          var error = function(message) { alert("Error: " + message); };

          // create
          window.plugins.calendar.createEvent(title,location,notes,startDate,endDate,success,error);    
    }           

    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    }

----------编辑:----------&gt;

  <div id="detailPage" data-role="page">
        <span onClick="createMyEvent(detailVar);" id="detailKalender">kalender</span>

  </div>

1 个答案:

答案 0 :(得分:0)

尝试:

 $('#detailPage').live('pageshow', function(event) {
    var id = getUrlVars()["id"];
    $.getJSON(serviceURL + 'getDetail.php?id='+id, displayDetail);
 });

function displayDetail(data) {
    var detailVar = data.item;
    console.log(detailVar);

// title            
    $('#detailTitel').text(detailVar.titel);
//location      
    $('#detailLocation').text(detailVar.location);
// date     
    $('#detailDatum').text(detailVar.datum);    
// end date     
    $('#detailDatumEnde').text(detailVar.datumEnde); 

createMyEvent(detailVar);   
}
//  Calendar
 // prep some variables
 function createMyEvent(detailVar){

      var startDate = new Date(detailVar.datum);    //    location(detailVar.datum); is not working
      var endDate = new Date(detailVar.datumEnde);  //    location(detailVar.datumEnde); is not working
      var title = detailVar.titel;                                                        //    location(detailVar.titel); is not working
      var location = detailVar.location;                                          //    location(detailVar.location); is not working
      var notes = "myNotes";
      var success = function(message) { alert("yes!"); };
      var error = function(message) { alert("Error: " + message); };

      // create
      window.plugins.calendar.createEvent(title,location,notes,startDate,endDate,success,error);    
}           

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

您需要将DetailVar传递给createMyEvent函数 另外,我从displayDetail函数中取消嵌套了CreateMyEvent和getURLVars函数 并在调用createMyEvent函数时添加,从displayDetail函数的末尾传入detailVar。