jQuery 事件未触发 [英] jQuery event not firing

查看:24
本文介绍了jQuery 事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个文件:

  • js_json.js -> 用于我的 json 代码
  • javascript.js -> 用于我的 javascript功能
  • index.php

这里是js_json.js的代码:

$(function(){
    $('#postTitle').change(function(){

        var title_id = $("#postTitle").val();


        $.ajax({
            type:"post",
            url:"proses.php",
            data:"title_id=" + title_id,
            dataType:"json",
            success:function(data){
                body="";
                //$.each(data, function(i,n){
                    //body = n['body'];    
                //});
                body += "<a href="javascript:void(0);" id="pesan" name="pesan" onClick="">Hola Test</a>";
                $(".postBody").empty();
                $(".postBody").append(body);
            },
            error:function(data){
                $(".postBody").empty();
                $(".postBody").append("NO Post Selected.");
            }

        });
        return false;
    });
});

这里是我的 javascript.js 代码:

$(function (){
    $("a[name=pesan]").click(function (){
        alert("holalalalalal.....!");    
    });
});

这里是 index.php 代码:

    //some code
    <body>
        <a href="javascript:void(0);" id="pesan" name="pesan">Hola Test 1</a>
        Posts : <br />
        <select name="title" id="postTitle">
            <option value="">Select Post...</option>
            <?php
                $sql = "SELECT id, title FROM posts ORDER BY title";
                $query = mysql_query($sql) or die(mysql_error());

                while($rows = mysql_fetch_array($query)){
                    print('<option value="' . $rows['id'] . '">' . $rows['title'] . '</option>');
                }
            ?>
        </select>
        <br />
        Body : <br />
        <div class="postBody">
            Will show the body of post.
        </div>
    </body>
</html>

我的问题是:

当我单击Hola Test 1"链接时,它可以正常工作并显示消息.问题是,在我单击选择选项并出现Hola 测试"链接后,然后单击该(Hola 测试")链接后,该消息没有出现并且 firebug 中没有错误...

谁能给我解释一下为什么...?谢谢...

Can somebody explain to me why...? Thank's...

推荐答案

click() 只会为 click 时页面中存在的元素绑定事件被调用(同样适用于没有选择器的 on()bind(),以及绑定快捷方式组中的所有其他方法;keydown(), change() 等).

click() will only bind the event for elements that exist in the page at the time click is called (the same goes for on() without a selector, bind(), and all the other methods in the shortcut-group for bind; keydown(), change() etc. ).

因为您的另一个元素是通过 AJAX 添加的一段时间后,处理程序没有绑定到它.

Because your other element is getting added via AJAX some time later, the handler isn't bound for it.

使用带有选择器的 .on() 代替,这将将事件绑定到选择器匹配的所有当前和未来元素.

Use .on() with a selector instead, which will bind the event to all current and future elements matched by the selector.

$(function (){
    $(document).on('click', 'a[name=pesan]', function () {
        alert("holalalalalal.....!");    
    });
});

由于 on() 是在 jQuery 1.7 中引入的,如果您使用的是 jQuery 的早期版本(就像问这个问题时存在的那些),您可以使用 live()delegate() 而不是 on;

Since on() was introduced in jQuery 1.7, if you're using earlier versions of jQuery (like those that existed when this question was asked), you can use live() or delegate() instead of on;

$(function (){
    $('a[name=pesan]').live('click', function () {
        alert("holalalalalal.....!");    
    });
});

这篇关于jQuery 事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆