日期:2014-05-17  浏览次数:20462 次

根据ajax返回的数据动态生成的td标签无法触发click事件
代码如下:
JScript code
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: "GET",
                url: "DisplayData.aspx",
                data: { key: "Display" },
                success: function (data, status) {
                    var json = eval("(" + data + ")");
                    //  alert(json);
                    var html = "<table border='1' cellspacing='1'><tr><td>用户ID</td><td>用户名</td>";
                    html += "<td>用户密码</td><td>编辑</td><td>删除</td></tr>";
                    for (var i = 0; i < json.length; i++) {
                        html += "<tr align='center'><td>" + json[i].testID + "</td>" + "<td>"
                         + json[i].testName + "</td>" + "<td>"
                         + json[i].testPassword + "</td>"
                         + "<td class='point' id='Edit' Editid='" + json[i].testID + "'>编辑</td>"
                         + "<td class='point' id='Delete' Deleteid='" + json[i].testID + "'>删除</td>";
                    }
                    html += "</table>";
                    $("#divContent").html(html);

                }
            })

            $("#Delete").bind("click", function () {
                var result = confirm('确定要删除吗?');
                 if (result == true) {
              
                var keyid = $("#this").attr("Deleteid");
                $.ajax({
                    type: "GET",
                    url: "DisplayData.aspx",
                    data: { key: "Delete", id: keyid },
                    success: function (data, status) {
                        if (data == true) {
                            alert('删除成功!');
                        }
                        else {
                            alert('删除失败!');
                        }
                    }
                })
              
                }
            })

            $("#BtnInsert").click(function () {
                //alert($("#Delete").attr("keyid"));
                $("#InsertDiv").show();
            })
        })

    </script>

调试了下,根本不会触发click事件,用的浏览器是IE8 ,搞了好久都没解决,各位帮帮忙,多谢了

------解决方案--------------------
ajax是异步的,你的hrml还没有出来就执行$("#Delete").bind了,那是肯定不行的
你可以改成同步的方法或者+ "<td class='point' onclick='zzz(this)' id='Delete' Deleteid='" + json[i].testID + "'>删除</td>";
------解决方案--------------------
$("#Delete").live("click",function(){

"要干的事";

});
------解决方案--------------------
HTML code

function MyBind()
{
$("#Delete").bind("click", function () {
                var result = confirm('确定要删除吗?');
                 if (result == true) {
              
                var keyid = $("#this").attr("Deleteid");
                $.ajax({
                    type: "GET",
                    url: "DisplayData.aspx",
                    data: { key: "Delete", id: keyid },
                    success: function (data, status) {
                        if (data == true) {