【问题标题】:this.submit is not triggering GET callthis.submit 没有触发 GET 调用
【发布时间】:2015-08-13 01:13:38
【问题描述】:

为什么没有来电

this.submit()

没有触发提交并调用我的控制器? Chrome中的开发工具说有错误并且该功能不存在!

$('form').submit(function(e) {
    e.preventDefault();
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById('SearchQuery').value;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            alert("Location found: " + results[0].geometry.location);
            $(this).submit();

        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
});

@using (Html.BeginForm("Search", "Home", FormMethod.Get)) {
<div class="form-group">
  @Html.TextBoxFor(model => model.SearchQuery, new { @class = "form-control"}) @Html.ValidationMessageFor(model => model.SearchQuery, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.Longitude, "", new { @class = "text-danger"
  }) @Html.TextBoxFor(model => model.ClassDate, "{0:MM/dd/yyyy}", new { @class = "datefield form-control" }) @Html.ValidationMessageFor(model => model.ClassDate, "", new { @class = "text-danger" }) @Html.Hidden("latitude", Model.Latitude) @Html.Hidden("longitude",
  Model.Longitude)
</div>
<button type="submit" id="submitSearch" class="btn btn-default">Search</button>
}

【问题讨论】:

  • 表单是如何创建的……指定的method是什么
  • html表单在哪里?请显示所有相关源代码。
  • 试试改成$(this).submit();
  • $(this).submit() 似乎也不起作用
  • 啊,新代码。在e.preventDefault(); 之后,添加myForm = $(this) 行。然后把$(this).submit();改成myForm.submit();

标签: javascript jquery html asp.net-mvc razor


【解决方案1】:

首先$(this).submit() 不是指您的表单。您在另一个与geocoder 关联的函数中,因此您尝试调用不存在的geocoder.submit() 函数。

即使您将表单元素保存到变量中,它也会创建一个无限循环。在该函数中,您首先取消提交。然后你再次调用该函数,再次取消它,再次调用该函数,再次取消它,依此类推,直到浏览器吐出假人。

相反,先执行你的逻辑,然后如果你想阻止表单提交,取消它(如果你不取消它,它会做一个正常的提交)

$('form').submit(function() {
  var geocoder = new google.maps.Geocoder();
  var address = $('#SearchQuery').val();
  var caSubmit = true; // assume we can
  geocoder.geocode({ 'address': address }, function (results, status) {
    if (status != google.maps.GeocoderStatus.OK) {
      canSubmit = false; // signal we should not submit
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
  return canSubmit; // if its false, the submit will be cancelled
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2021-08-18
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    相关资源
    最近更新 更多