this.submit没有触发GET调用

时间:2015-08-13 01:13:39

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

为什么没有通话

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>
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

首先$(this).submit()不是指您的表单。您的内部其他功能与geocoder相关联,因此您尝试调用不存在的.submit() geocoder函数。

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

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

$('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
});