在My Razor View中,jquery在进行ajax调用后不起作用?

时间:2013-11-11 10:34:00

标签: jquery ajax asp.net-mvc-4 razor webforms

我正在发回帖子以使用ajax获取部分视图。 我正在使用以下代码。

<script type ="text/javascript" >
       $('#Retrieve').click(function () {
           $('form').get(0).setAttribute('action', 'Search');
           //                      $('form').submit();
           var formSubmit = $('form');
           var datTab;
           $.ajax({
               url: "/AuthorityGrid/Search",
               type: "POST",
               data: formSubmit.serialize(),
               success: function (data) {
                   datTab = data;
               },
               complete: function () {
                   $('#DivSearchGrid').html(datTab);

               }

           })
           return false;
       });
   </script>
控制器中的

操作方法返回具有新值的网格。我的问题是,在ajx调用完成后,我页面中的其他jquery事件停止工作。事件代码

<script type="text/javascript">

        $(function () {
            //$('th[scope|="col"]').resizable();
            $("#resultGrid > tbody").selectable({
                selected: function (event, ui) {
                    if (ui.selected.cells != null) {
                        var strAmount = ui.selected.cells(6).innerText;
                        var Amount = strAmount.replace(/,/gi, "");
                        var keyValue = "AuthorityLevel1=" + ui.selected.cells(11).innerText + ",AuthorityLevel2=" + ui.selected.cells(12).innerText + ",TcmAccount=" + ui.selected.cells(2).innerText + ",TcmType=" + ui.selected.cells(10).innerText + ",Rating=" + ui.selected.cells(5).innerText + ",Amount=" + Amount + ",AuthorityGridKey=" + ui.selected.cells(9).innerText + ",CagName=" + ui.selected.cells(3).innerText
                        var keyValModify = ui.selected.cells(11).innerText + "," + ui.selected.cells(10).innerText + "," + ui.selected.cells(12).innerText + "," + ui.selected.cells(5).innerText + "," + ui.selected.cells(2).innerText + "," + Amount + "," + ui.selected.cells(3).innerText + "," + ui.selected.cells(9).innerText
                        $('#CancelViewParam').val(keyValue);
                        $('#ModifyViewParam').val(keyValModify);

                    }
                }
            });
        });
    </script>

此函数从网格中选择一行,并将所选值放在隐藏字段中。

在此函数的ajax call.code之后,打开弹出窗口的功能也无效。

$(function () {
 $("#DivSearch").dialog({ autoOpen: false, height: "600", width: "600", dialogClass: "myRatingHelp", modal: true });
 $('#bRatingHelperDivSearch').live('click',function () { $('#DivSearch').dialog('open'); });
 $('#DivSearchRating_bOk').click(function () {
 $("#InputAuthorityGridSearch_Rating").val($("#hidRating").val());
 $("#DivSearch").dialog('close');
 });
 $('#DivSearchRating_bCancel').click(function () {
 $("#DivSearch").dialog('close');
 });
 });

这些函数在ajax调用之前运行良好,但在进行ajax调用后都停止工作。 我坚持这个。请帮帮我。

1 个答案:

答案 0 :(得分:3)

问题是当HTML被替换时,元素会丢失它们的绑定。将事件绑定到正文或#DivSearchGrid'

$('#DivSearchGrid').on("click", "#DivSearchRating_bOk", "function () {
    $("#InputAuthorityGridSearch_Rating").val($("#hidRating").val());
    $("#DivSearch").dialog('close');
 });

$('body').on("click", "#DivSearchRating_bOk", "function () {
    $("#InputAuthorityGridSearch_Rating").val($("#hidRating").val());
    $("#DivSearch").dialog('close');
 });