如何使函数只在表行上双击

时间:2017-11-28 11:36:45

标签: javascript jquery asp.net

我添加了一个jquery函数来处理表行双击。由于某些原因,函数在双击上工作。有了jquery的新功能,我无法找到我所遗漏的内容。
我正在使用 Jquery函数:

 $(document).ready(function () {

        function addRowHandlers() {
            var table = document.getElementById("shTable");
            var rows = table.getElementsByTagName("tr");
            for (i = 0; i < rows.length; i++) {
                var currentRow = table.rows[i];
                var createClickHandler =
                    function (row) {
                        return function () {
                            var cell1 = row.getElementsByTagName("td")[1];
                            //------------------------------------------
                            $.ajax({
                                type: "POST",
                                url: "BankSettings.aspx/GetBranchDetails",
                                data: "{'BranchCode':'" + cell1.innerHTML + "'}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: OnSuccess,
                                failure: function (response) {
                                    alert(response.d);
                                }

                            });
                            function OnSuccess(response) {
                                var data = response.d;                                                                       
                                var myData = JSON.parse(data);
                                $('#<%=txtbranchname.ClientID%>').val(myData.BranchName);
                                $('#<%=txtbranchcode.ClientID%>').val(myData.BrCode);
                                $('#<%=txtreglocal.ClientID%>').val(myData.RegLocal);
                                $('#<%=txtregstate.ClientID%>').val(myData.RegState);
                                $('#<%=txtregcentral.ClientID%>').val(myData.RegCentral);
                            }

                            ///-----------------------------------------
                        };
                    };

                    currentRow.dblclick= createClickHandler(currentRow);
                }
            }
        window.onload = addRowHandlers();
    });

1 个答案:

答案 0 :(得分:0)

您正在分配createClickHandler返回值,而不是功能参考本身。

替换

currentRow.dblclick= createClickHandler(currentRow);

使用(plain JS dblclick

currentRow.dblclick = function ( event ){ createClickHandler( event.target ) };
使用jquery's dblclick

$( currentRow ).dblclick ( function( event ){ createClickHandler( event.target ) } );