MVC3&打开对话框时,JQuery 对话框不会提交表单

发布于 2024-12-19 04:54:24 字数 1805 浏览 2 评论 0原文

我有一个用于在 MVC3 应用程序中搜索的对话框。对话框上的搜索按钮发布到 MVC3 控制器操作,该操作返回搜索结果的 JSON,然后将其解析为 HTML 表。当用户单击对话框上的“搜索”按钮时,所有这些都可以正常工作。

但是,在某些情况下,我希望对话框打开后自动开始搜索。不幸的是,这不起作用。用户必须实际单击“搜索”按钮才能启动搜索。

我的代码看起来像这样:

$('#RepSearchDialog').dialog({
          autoOpen: true,
          width: 1050,
          height: 500,
          resizable: false,
          title: 'Rep Search',
          modal: true,
          open: function () {
            $('.ui-dialog-titlebar').hide();
            $('#RepSearchStoreId').val($('#StoreId').val());

            // This part doesn't work, not sure why
            //RepSearchDialog_SearchForReps();
          }
        });

搜索按钮有这样的 JS:

 $('#RepSearchButton').click(function (e) {
      RepSearchDialog_SearchForReps();
    });

RepSearchDialog_SearchForReps 看起来像这样: RepSearchDialog_SearchForReps

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit(function () {
      $.ajax({
        url: this.action,
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: $(this).serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
      });
      return false;
    });
  }

  function RepSearchDialog_SearchForRepsComplete(response, status, xhr) {
    $('#RepSearchButton').unbind(); // NECESSARY, or you will get stacked calls!!
    if (status == "error") {
      alert('failed');
    }
    else {
      LoadRepSearchResults(response);
    }
  }

调用只是对 MVC3 控制器进行 AJAX 调用,并将返回值附加到对话框内托管的 DIV 中的 HTML 表。当用户单击“搜索”按钮时,所有这些都会起作用。但尝试在 OPEN 函数中自动启动此功能却不会。有什么线索吗?

I have a dialog that is used for searching in an MVC3 application. The Search button on the dialog posts to an MVC3 Controller action that returns JSON of search results which then get parsed into an HTML table. All of that works fine when the user clicks the Search button on the dialog.

However, in some circumstances, I want the search to kick off automatically as soon as the dialog opens. Unfortunately, this doesn't work. The user has to physically click on the Search button in order to initiate the Search.

My code looks like such:

$('#RepSearchDialog').dialog({
          autoOpen: true,
          width: 1050,
          height: 500,
          resizable: false,
          title: 'Rep Search',
          modal: true,
          open: function () {
            $('.ui-dialog-titlebar').hide();
            $('#RepSearchStoreId').val($('#StoreId').val());

            // This part doesn't work, not sure why
            //RepSearchDialog_SearchForReps();
          }
        });

The search button has JS like this:

 $('#RepSearchButton').click(function (e) {
      RepSearchDialog_SearchForReps();
    });

And the RepSearchDialog_SearchForReps looks like this:

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit(function () {
      $.ajax({
        url: this.action,
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: $(this).serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
      });
      return false;
    });
  }

  function RepSearchDialog_SearchForRepsComplete(response, status, xhr) {
    $('#RepSearchButton').unbind(); // NECESSARY, or you will get stacked calls!!
    if (status == "error") {
      alert('failed');
    }
    else {
      LoadRepSearchResults(response);
    }
  }

The RepSearchDialog_SearchForReps call simply makes an AJAX call to the MVC3 controller and appends the returning values to an HTML table in a DIV hosted inside the Dialog. When the user clicks the Search button all of this works. But trying to kick this off automatically in the OPEN function does not. Any clues why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2024-12-26 04:54:24

您的搜索按钮起作用的原因是它仍然使用正常的帖子以及调用您的 JavaScript。您需要将其更改为:

$('#RepSearchButton').click(function (e) {
    e.preventDefault();
    RepSearchDialog_SearchForReps();
});

这里 e.preventDefault() 将阻止本机提交事件发生。

另一个问题是您的RepSearchDialog_SearchForReps。按照您的设置方式,您每次都会将 $.ajax 调用绑定到 submit 事件调用 RepSearchDialog_SearchForReps。为此,您有两种选择。你可以简单地这样做:

function RepSearchDialog_SearchForReps() {
    var form = $('#RepSearchForm');
    $.ajax({
        url: form.attr('action'),
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: form.serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
    });
}

或者这样

// this goes in your document ready code elsewhere...
$(function() {
    $('#RepSearchForm').submit(function () {
        $.ajax({
            url: this.action,
            type: "POST",
            cache: false,
            dataType: 'text json',
            data: $(this).serialize(),
            success: RepSearchDialog_SearchForRepsComplete,
            error: function (request, status, error) {
              alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
            }
        });
        return false;
    });
});

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit();
}

The reason that your search button is working is that it is still using the normal post as well as calling your javascript. You need to change it to this:

$('#RepSearchButton').click(function (e) {
    e.preventDefault();
    RepSearchDialog_SearchForReps();
});

Here the e.preventDefault() will stop the native submit event from occurring.

Another problem is your RepSearchDialog_SearchForReps. The way you have it set up, you are binding the $.ajax call to a submit event every time RepSearchDialog_SearchForReps is called. You have two options for this. You can simply do this:

function RepSearchDialog_SearchForReps() {
    var form = $('#RepSearchForm');
    $.ajax({
        url: form.attr('action'),
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: form.serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
    });
}

or this

// this goes in your document ready code elsewhere...
$(function() {
    $('#RepSearchForm').submit(function () {
        $.ajax({
            url: this.action,
            type: "POST",
            cache: false,
            dataType: 'text json',
            data: $(this).serialize(),
            success: RepSearchDialog_SearchForRepsComplete,
            error: function (request, status, error) {
              alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
            }
        });
        return false;
    });
});

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文