jQuery preventDefault()不起作用

时间:2013-05-16 09:54:25

标签: c# jquery asp.net preventdefault

我创建了一个函数,每次单击动态创建的ImageButton时都会调用它。 但是precenDefault()不起作用。除防止默认外,该功能正常工作。

的jQuery

<script type="text/javascript">
    function EmpDelete(event, id) {
        event.preventDefault();
        $("#bName").text(id);        
        $('#dialog').dialog({            
            title: "Delete Employee?",
            modal: true,
            width: 500,
            height: 250,
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                    return false;
                },
                "Continue": function () {
                    $(this).dialog("close");
                    return true;
                }
            }
        });        
    };    
</script>

ASPX.CS

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
        e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgDelete",
                CssClass = "imgDelete",
                OnClientClick = "return EmpDelete(" + e.Row.Cells[iFNmae].Text + ");"
            });
    }
}

3 个答案:

答案 0 :(得分:0)

事件需要传递给函数。该函数需要两个参数,但它只传递一个参数。

 OnClientClick = "return EmpDelete(e," + e.Row.Cells[iFNmae].Text + ");"

完整资源

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
        e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgDelete",
                CssClass = "imgDelete",
                OnClientClick = "return EmpDelete(e" + e.Row.Cells[iFNmae].Text + ");"
            });
    }
}

答案 1 :(得分:0)

第一个错误是您不能以这种方式从该函数调用preventDefault。设置呼叫的方式最好在呼叫结束时返回false,这样点击只会打开对话框而不会继续;

现在按钮OKCancel将在函数已经返回到ImageButton之后运行,因为对话框只是打开并返回 - 不等待按钮返回。

<script type="text/javascript">
    function EmpDelete(event, id) 
    {
        // this is not correct because you do not have the event for preventDefault
        // event.preventDefault();
        $("#bName").text(id);        
        $('#dialog').dialog({            
            title: "Delete Employee?",
            modal: true,
            width: 500,
            height: 250,
            // that buttons here will not work as you belive !
            // they are not return and let the post back occurs after the event.
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                    return false;
                },
                "Continue": function () {
                    $(this).dialog("close");
                    return true;
                }
            }
        });        
      // you need to return here false.
      return false;
    };    
</script>

这里的解决方案是保存帖子后退点击,并在对话框关闭时使用它。

只需返回confirm

即可
OnClientClick = "return confirm('are you sure to delete this employee ?')'

甚至更好的方法是在使用控件类时吸引确认,而不是将其添加到代码后面。

答案 2 :(得分:0)

我在jQuery“继续”按钮和

上使用__doPostBack(uniqueID, '');解决了这个问题

在Page_Load()事件上我添加了

 if (!Page.IsPostBack)
    {
        ClientScript.GetPostBackEventReference(gvEmployee, string.Empty);
    }

我在Gridview_RowDataBound事件上调用了jQuery Dialog

e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgEmpDelete" + e.Row.Cells[iID].Text,
                **OnClientClick = "javascript:return rowAction(this.name)",**
                ClientIDMode = System.Web.UI.ClientIDMode.Static
            });

希望它可以帮助任何人。