简体   繁体   English

无法获取jQuery单击事件在ajax生成的HTML上工作

[英]Cant get jquery click event working on ajax generated HTML

This is part of another query I have here but really stuck.... 这是我在这里遇到的另一个查询的一部分,但确实卡住了。

I cant get jquery clcik event to work on some ajax generated HTML. 我无法获取jquery clcik事件来处理一些由ajax生成的HTML。

I've read all the articles and questions/answers but she wont work for me .. all help greatly appreciated.... 我已经阅读了所有文章和问题/答案,但她不会为我工作..所有帮助都将不胜感激....

JS JS

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

$(document).ready(function(){    
    $("document").on("click", ".clickme", function() {
        alert("click!");
    });

ajax 阿贾克斯

$.ajax({

        url: 'http://www.websites.com/lol.php',

        dataType: 'jsonp',

        jsonp: 'jsoncallback',

        timeout: 5000,

        success: function(data, status){            
            $.each(data, function(i,item){    
                 var balance = '<p>'+item.itemm + ' '+ item.amount + ' '+item.status  +  ' ' + '<input value="delete "type="button" id="clickme">'
                 + '</p><hr>';
                total = total + parseInt(item.amount);

                $("#mylbl2").append(balance);

HTML HTML

<div class="col span_1_of_3">
   <b>VISA</b>
   <span id="mylbl2"></span>
   <b>TOTAL: <span id="mylbl3"></span></b><br/><br/>
</div>

Currently all I'm trying to do is get a click event working on the delete button...once I can get this working I can move on get the id and pass to a delete function... I'm stuck on getting the click event working. 目前,我要做的就是让click事件在Delete按钮上工作...一旦我能正常工作,就可以继续获取ID并传递给Delete函数...我一直在坚持点击事件正常运行。

Could it just be punctuation or something silly that I'n overlooking?? 仅仅是标点符号还是我忽略的傻事?

document is not a selector. document不是选择器。 So, it shouldn't be wrapped in " " . 因此,不应将其包装在" " Do the following 请执行下列操作

$(document).on("click", "#clickme", function() {
    alert("click!");
});

try this 尝试这个

$(document).ready(function(){    
    $(document).on("click", "#clickme", function() {
        alert("click!");
    });
});

Are you trying to add a click listener to the class of the element? 您是否要向元素的类添加点击侦听器? Cause in your code you're adding it to the CLASS while it seems like you're referring to the ID. 因为在您的代码中您正在将其添加到CLASS中,而您似乎在引用ID。 If you are willing to add multiple items with only the id change, it might be better to do: 如果您只想更改ID即可添加多个项目,则最好这样做:

$(".clickme").click(function(e) 
{
    alert(e.target.id);
});

And change in your ajax call 并改变你的ajax电话

<input value="delete "type="button" id="clickme">

to

<input value="delete "type="button" class="clickme">

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

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