Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ASP.NET MVC中加载AJAX的PartialView-脚本运行多次_Javascript_Jquery_Ajax_Asp.net Mvc 4_Partial Views - Fatal编程技术网

Javascript 在ASP.NET MVC中加载AJAX的PartialView-脚本运行多次

Javascript 在ASP.NET MVC中加载AJAX的PartialView-脚本运行多次,javascript,jquery,ajax,asp.net-mvc-4,partial-views,Javascript,Jquery,Ajax,Asp.net Mvc 4,Partial Views,我有一些局部视图,它们都有一些类似的类(.update、.save、.cancel),但它们的用途不同。在每个页面上,我编写一个脚本来处理这些类,并使用ajax加载PartialView 除了单击按钮重新加载页面外,其他所有操作都正常,该页面中的脚本运行两次,再次单击,脚本运行3次 $('#gridcontainer').load("/webdata/bank", {}, function () { alert('Load page'); // alert run 1 time when

我有一些局部视图,它们都有一些类似的类(.update、.save、.cancel),但它们的用途不同。在每个页面上,我编写一个脚本来处理这些类,并使用ajax加载PartialView

除了单击按钮重新加载页面外,其他所有操作都正常,该页面中的脚本运行两次,再次单击,脚本运行3次

$('#gridcontainer').load("/webdata/bank", {}, function () {
    alert('Load page'); // alert run 1 time when page load, it normal
    $('body').on('click', '.save', function () { // this function run n time when i load page n time.
          var c = confirm('Do you want save?')
          if (c) {
               $(this).val('Edit').removeClass('save').addClass('update');
               var v = $(this).closest('tr').find('td');
               v.eq(1).find('span').removeClass('hide');
               v.eq(1).find('input').attr('type', 'hidden');
               v.eq(2).find('input.cancel').attr('type', 'hidden');

               var id = $(this).data('id');
               // more code
          }
     });
}

您应该在重新加载时删除任何以前附加的事件,请禁用Jquery

$('#gridcontainer').load("/webdata/bank", {}, function () {
    alert('Load page'); // alert run 1 time when page load, it normal
    $('body').off( "click", ".save").on('click', '.save', function () { // this function run n time when i load page n time.
          var c = confirm('Do you want save?')
          if (c) {
               $(this).val('Edit').removeClass('save').addClass('update');
               var v = $(this).closest('tr').find('td');
               v.eq(1).find('span').removeClass('hide');
               v.eq(1).find('input').attr('type', 'hidden');
               v.eq(2).find('input.cancel').attr('type', 'hidden');

               var id = $(this).data('id');
               // more code
          }
     });
}

请为您的主页和部分视图发布HTML。问题似乎很明显,每次加载内容和复合事件处理程序时,您都在加载相同的脚本