无法获得功能。("点击")

时间:2014-05-03 21:45:53

标签: jquery

我无法使用“span id =”detailKalender“获取”createMyEvent“函数。 每次加载“detailPage”时,都会触发createMyEvent函数。它应该只在点击“#detailKalender”时点击。有什么建议吗?

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

非常感谢任何帮助!

  // JavaScript
  $('#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.datumStart);    
        var endDate = new Date(detailVar.datumEnde);   
        var title = detailVar.titel;                                                   
        var location = detailVar.location;                                          
        var notes = "myNotes";
        var success = function(message) { alert("my alert"); };
        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;
  }

HTML:

    <div id="detailPage" data-role="page">
    <span id="detailKalender">kalender</span>
    <script>          
        $( "#detailKalender" ).on("click", createMyEvent);
    </script>   
    </div>

1 个答案:

答案 0 :(得分:3)

加载页面时,会调用以下函数

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

因此,当加载JSON时,它会调用回调函数,在本例中为displayDetail() 该功能看起来像

function displayDetail(data) {

    ... code

    createMyEvent(detailVar);   
}

并且你有它,你在每个页面加载上调用createMyEvent()函数