简体   繁体   English

jQuery .on()事件无法从Ajax加载的内容中获取数据

[英]jQuery .on() event cannot grab data from Ajax loaded content

Hey folks I have content being displayed on a page by using jQuery and Ajax to query my php page. 嘿,我有一些内容通过使用jQuery和Ajax查询我的php页面显示在页面上。 On click I check for all elements with the class edit. 单击时,我检查所有带有类编辑的元素。 I am trying to then grab the unique id of the link that was chosen. 然后,我试图获取所选链接的唯一ID。 I know it should be this.id but it returns nothing. 我知道应该是this.id,但它什么也不返回。 Does anyone have any insight on how to dynamically check the clicked link and get a stored value in any way? 是否有人对如何动态检查单击的链接并以任何方式获取存储值有任何见解?

The bellow works for getting a link clicked. 波纹管用于获得链接单击。 but cannot get the id still. 但仍无法获取ID。

$("body").on("click", ".edit", function(){
    var id = this.id;
    alert($(this).val());
}); 

Here is the HTML that is being put on the page via Ajax 这是通过Ajax放在页面上的HTML

<div class="row">
             <div class="small-2 large-2 columns">3</div>
             <div class="small-10 large-6 columns">Andrew</div>
             <div class="small-6 large-2 columns">2013-08-12</div>
             <div class="small-6 large-2 columns edit"><a href="#" id="e3"> Edit </a></div>
        </div>

Solved solution was to use '.edit > a' as the selector. 解决的解决方案是使用“ .edit> a”作为选择器。 I had edit as the class of the div, not the anchor tag. 我已经将div的类作为编辑器,而不是锚标记。 Thanks for all the help folks! 感谢大家的帮助!

The sample HTML helped: HTML示例有助于:

You need to select the anchor within the .edit div. 您需要在.edit div中选择锚点。

$("body").on("click", ".edit > a", function () {
    var id = this.id;
    alert(id);
});

JSFiddle: http://jsfiddle.net/QckbT/ JSFiddle: http : //jsfiddle.net/QckbT/

Does this works ? 这行得通吗?

$("a.edit").on("click", function(){
     alert($(this).val());
});
alert($(this).val());

与身体有关,而不与#id有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM