为什么ajax下的javascript actionlink函数在mvc中不起作用

时间:2015-12-19 05:06:02

标签: javascript ajax asp.net-mvc

在ajax get方法下我需要以编程方式生成表。为什么actionlink不能与我的表一起使用

ajax方法

       $(document).ready(function () {
//click event
            $('.delete-logo').on('click', function () {

                var id = $(this).data('key');
                alert(id);
            });
//click event               
            $('.edit-logo').on('click', function () {

                var id = $(this).data('key');
                alert(id);
            });

            $('.submitDetailForm').on('click', function () {                

            //get value from control
                var ProductID = $('#ProductID').val();
                var Qty = $('#Qty').val();
                var Unit = $('#Unit').val();
                var Amount = $('#Amount').val();
                var ICMS = $('#ICMS').val();
                var IPI = $('#IPI').val();
                var ProductName = $('#ProductID option:selected').text();


                var booksDiv = $("#booksDiv");
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: '@Url.Action("AddToCard", "Sales")',
                    data: { ProductID: ProductID, ProductName: ProductName, Qty: Qty, Unit: Unit, Amount: Amount, ICMS: ICMS, IPI: IPI },
                    success: function (data) {
                        console.log(data);
                        var result = "";
                        booksDiv.html('');
                        $.each(data, function (SalesOrderID, OrderDetails) {

                            result += '<tr> <td>' + OrderDetails.Name + '</td>' +
                                '<td>' + OrderDetails.Qty + '</td>' +
                                '<td>' + OrderDetails.Unit + '</td>' +
                                '<td>' + OrderDetails.Amount + '</td>' +
                                '<td>' + OrderDetails.ICMS + '</td>' +
                                '<td>' + OrderDetails.IPI + '</td>' +
                                '<td><a class="edit-logo" data-key=' + OrderDetails.SalesOrderDetailID + ' href="javascript:void(0);">' + 'Edit' + '</a></td>' +
                                '<td><a class="delete-logo" data-key=' + OrderDetails.SalesOrderDetailID + ' href="javascript:void(0);">' + 'Delete' + '</a></td>' +
                                ' </tr>';
                        });
booksDiv.html(result);

                    },
                    error: function (xhr, AJAXOptions, thrownError) {
                        alert('Failed to retrieve books.');
                    }
                });
            });
        });

超链接内容

    '<td><a class="edit-logo" data-key=' + OrderDetails.SalesOrderDetailID + ' href="javascript:void(0);">' + 'Edit' + '</a></td>' 

'<td><a class="delete-logo" data-key=' + OrderDetails.SalesOrderDetailID + ' href="javascript:void(0);">' + 'Delete' + '</a></td>' 

超链接在浏览器中完美显示但无法调用点击事件

为什么我的actionlink click事件没有被触发?

1 个答案:

答案 0 :(得分:1)

添加动态内容时,您需要使用事件委派(使用.on()功能)

$('#booksDiv').on('click', '.delete-logo', function() {
  ....
});
$('#booksDiv').on('click', '.edit-logo', function() {
  ....
});

其中id="booksDiv"元素是首次生成页面时最近的祖先。

附注:您可以简单地使用data: $('form').serialize(),

,而不是手动生成javascript对象