Javascript 如何引用动态创建的变量

Javascript 如何引用动态创建的变量,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我想知道是否可以引用一个动态创建的变量,如果可以,如何引用 我在这个网站上创建了许多表单,这些表单的底部有p个元素,如果我点击了这个按钮,我想传输在这个表单中创建的变量IDJB。 我在代码中用命令标记了变量 $document.readyfunction{ var Id=sessionStorage.getItem'Id'; var状态=0; var nummer=0; $.ajax{ 类型:POST,, 网址:http://localhost/jQuery&PHPnew/Markt.N.ph

我想知道是否可以引用一个动态创建的变量,如果可以,如何引用

我在这个网站上创建了许多表单,这些表单的底部有p个元素,如果我点击了这个按钮,我想传输在这个表单中创建的变量IDJB。 我在代码中用命令标记了变量

$document.readyfunction{ var Id=sessionStorage.getItem'Id'; var状态=0; var nummer=0; $.ajax{ 类型:POST,, 网址:http://localhost/jQuery&PHPnew/Markt.N.php, 数据:{ 状态:状态 }, 成功:functiondata{ var anzahl=数据; 状态=1; 而anzahl>nummer{ numer++; $.ajax{ 类型:POST,, 网址:http://localhost/jQuery&PHPnew/Markt.N.php, 数据:{ 努默:努默, 状态:状态 }, 成功:functiondata{ var Daten=JSON.parsedata; var Ausgabebereich=document.getElementById'main'; var IdJB=Daten.id; window.IdJB=IdJB;//此变量!!!!!!!!!!!!! var f=document.createElementform; var pInhalt=document.createElement'p'; var Inhalt=document.createTextNodeDaten.Inhalt; pInhalt.appendChildInhalt; f、 附属品暂停; var pDatum=document.createElement'p'; var Inhalt=document.createTextNodeDaten.datum; pDatum.appendChildInhalt; f、 附属物; var pUhrzeit=document.createElement'p'; var Inhalt=document.createTextNodeDaten.uhrzeit; pUhrzeit.appendChildInhalt; f、 普赫泽特; var pGehalt=document.createElement'p'; var Inhalt=document.createTextNodeDaten.gehalt; pGehalt.appendChildInhalt; f、 附加的ChildPgehalt; var pDauer=document.createElement'p'; var Inhalt=document.createTextNodeDaten.dauer; pDauer.appendChildInhalt; f、 儿童家长; var pAdresse=document.createElement'p'; var Inhalt=document.createTextNodeDaten.adresse; 帕德瑞斯·帕迪尼哈特; f、 附属物; var pNam_ersteller=document.createElement'p'; var Inhalt=document.createTextNodeDaten.nam_ersteller; pNam_ersteller.appendChildInhalt; f、 帕纳姆·厄斯泰勒; var bInhalt=document.createElement'button'; var Inhalt=document.createTextNodeSenden; bInhalt.appendChildInhalt; bInhalt.setAttributetype,按钮; bInhalt.setAttributeonclick,zuJB; f、 附属物; Ausgabebeerich.appendChildf; $document.on'click',button',函数{ sessionStorage.setItem'IdJB',IdJB;//在这里!!!!!!!!!!!!! alertIdJB; window.location=http://localhost/jQuery&PHPnew/JobBlock.html; }; } } } } } $sub.clickfunction{ window.location=http://localhost/jQuery&PHPnew/Markt.html; } } 乔布斯 使现代化
我可能误解了您的问题,但如果不是这样的话:为了引用动态创建的元素,只需要在创建元素并将其放入DOM后实例化click处理程序。处理此问题的一个简单方法是使用函数生成标记,如下所示:

function appendTag(parent, child){
  parent.append(child)
  child.click(function(){
    //Click code here
  });
}

不要使用全局变量。将IdJB放入按钮的属性中。可以使用jQuery.data方法进行此操作

另外,不要每次通过循环添加事件处理程序。使用事件委派时,只需添加一次处理程序

$document.readyfunction{ $document.on'click',button',函数{ var IdJB=$this.dataIdJB; sessionStorage.setItem'IdJB',IdJB; alertIdJB; window.location=http://localhost/jQuery&PHPnew/JobBlock.html; }; var Id=sessionStorage.getItem'Id'; var状态=0; var nummer=0; $.ajax{ 类型:POST,, 网址:http://localhost/jQuery&PHPnew/Markt.N.php, 数据:{ 状态:状态 }, 成功:functiondata{ var anzahl=数据; 状态=1; 而anzahl>nummer{ numer++; $.ajax{ 类型:POST,, 网址:http://localhost/jQuery&PHPnew/Markt.N.php, 数据:{ 努默:努默, 状态:状态 }, 成功:functiondata{ var Daten=JSON.parsedata; var Ausgabebereich=document.getElementById'main'; var IdJB=Daten.id; window.IdJB=IdJB;//此变量!!!!!!!!!!!!! var f=document.createElementform; var pInhalt=document.createElement'p'; var Inhalt=document.createTextNodeDaten.Inhalt; pInhalt.appendChildInhalt; f、 附属品暂停; var pDatum=document.createElement'p'; var Inhalt=document.createTextNodeDaten.datum; pDatum.appendChildInhalt; f、 附属物; var pUhrzeit=document.createElement'p'; var Inhalt=document.createTextNodeDaten.uhrzeit; pUhrzeit.appendChildInhalt; f、 普赫泽特; var pGehalt=document.createElement'p'; var Inhalt=document.createTextNodeDaten.gehalt; pGehalt.appendChildInhalt; f、 附加的ChildPgehalt; var pDauer=document.createElement'p'; var Inhalt=document.createTextNodeDaten.dauer; pDauer.appendChildInhalt; f、 儿童家长; var pAdresse=document.createElement'p'; var Inhalt=document.createTextNodeDaten.adresse; 帕德瑞斯·帕迪尼哈特; f、 附属物; var pNam_ersteller=document.createElement'p'; var Inhalt=document.createTextNodeDaten.nam_ersteller; pNam_ersteller.appendChildInhalt; f、 帕纳姆·厄斯泰勒; var bInhalt=document.createElement'button'; var Inhalt=document.createTextNodeSenden; bInhalt.appendChildInhalt; bInhalt.setAttributetype,按钮; bInhalt.setAttributeonclick,zuJB; f、 附属物; $bInhalt.data'IdJB',IdJB; Ausgabebeerich.appendChildf; } } } } } $sub.clickfunction{ window.location=http://localhost/jQuery&PHPnew/Markt.html; } } 乔布斯 使现代化
因此,当您动态添加按钮单击事件时,您将其添加到页面上的所有按钮中。这就是为什么当你点击一个它会运行3次

这可以通过以下代码行中的按钮文本看到:

$(document).on('click', 'button', function() {
           .....
     });
要解决此问题,您需要使单击侦听器特定于该按钮。您可以通过制作更具体的按钮选择器来实现这一点。虽然这不是一个完美的解决方案,但一种方法是给每个按钮一个唯一的ID。类似于button,每次创建新按钮时,这里都有一个不同的数字。然后,您可以执行以下操作:

    $(document).on('click', '#button-##', function() {
           .....
     });
再次替换为相应的Id值


编辑:实际上,你可以使用由发布的答案@Laif,而不是使用$document。随叫随到只需执行b.单击

动态变量几乎总是一个坏主意。使用带有动态键的对象。@Barmar好的,我该怎么做?你是在问动态变量还是动态元素?有关动态元素的事件,请参阅。你的问题是单击事件没有触发,警报框是空的,还是控制台中有错误?非常感谢你我喜欢这个社区,感谢你的耐心也谢谢你,谢谢。谢谢你的帮助