使用JQuery Click()时无法传递事件

时间:2018-08-29 17:22:34

标签: javascript jquery event-handling click

使用下面的单击功能时,我无法从event.data获取数据。如果我在click(smedata,function(event){})中放入“事件”,则不会触发。如果我删除事件,则会触发。我过去已经做过一百次了,但是无论出于什么原因,这次都没有用。我所能想到的是,这段代码是在关闭对话窗口的回调过程中生成的,这多少有些干扰。感谢任何帮助。

//This is a callback function in a library that gets called after a dialogue 
//window from SharePoint SP.UI.Dialogue gets closed.

OESme.prototype.CloseCallback = function(result, target){
  console.log("callback");

  try{
    var targetArray = target.split(";#");
  }
  catch(e){

  }
  
  //object to be passed to click event
  var smedata = {
    smejson: target,
    id: targetArray[0],
    title: targetArray[1],
    rank: targetArray[2],
    firstname: targetArray[3],
    lastname: targetArray[4]
  }

  var smeID = "smedata" + smedata.id;
  var smeIDJQ = "#" + "smedata" + smedata.id;

  $("#SMEAddedBox").append(
    '<div class="smeitem">' + 
      '<span id="' + smeID + '">x</span>' + smedata.title + 
    '</div>'
  );


   //******************* 		
  //When clicking the x it is suppose to remove itself
  //If event is a parameter in the function, it won't fire, if event is removed it fires, but I can't use the object I am trying to pass
  //********************

  $(smeIDJQ).click(smedata, function(event){
      console.log(event.data);
      $(this).parent().remove();

  });
    		     
}

2 个答案:

答案 0 :(得分:0)

您似乎没有正确引用obj.toString()。另外,请改用$(smeIDJQ)。像.on

答案 1 :(得分:0)

我所有的代码都是正确的。该问题与SharePoint SP.UI.Dialogue.js有关。所有的javascript和jQuery都是正确的,但它正在“对话”窗口中运行。基本上有两个单独的页面在一个页面上运行。在回叫后对话框窗口关闭时,Internet Explorer 11中的控制台中断,并且没有意识到应该专注于父页面而不是子页面。因此,它不会收到应有的控制台日志。关闭开发人员工具并重新打开(F12),可使窗口正确地重新调整父对象的位置。

相关问题