this.submit不触发GET调用 [英] this.submit is not triggering GET call

查看:203
本文介绍了this.submit不触发GET调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不是调用

  this.submit()

不触发提交,并呼吁我的控制器?
在Chrome的开发工具说有一个错误,该函数不存在!

\r
\r

$(形式)。递交(函数(五){\r
    亦即preventDefault();\r
    VAR地理codeR =新google.maps.Geo codeR();\r
    VAR地址=的document.getElementById('SEARCHQUERY')值。\r
    地理coder.geo code({'地址':地址},功能(结果状态){\r
        如果(状态== google.maps.Geo coderStatus.OK){\r
            警报(位置找到:+结果[0] .geometry.location);\r
            $(本).submit();\r
\r
        }\r
        其他{\r
            警报(地理code不成功,原因如下:+状态);\r
        }\r
    });\r
});

\r

\r
\r

\r
\r

@using(Html.BeginForm(搜索,家,FormMethod。获得)){\r
< D​​IV CLASS =表单组>\r
  @ Html.TextBoxFor(型号=> model.SearchQuery,新{@class =表格控})@ Html.ValidationMessageFor(型号=> model.SearchQuery,新{@class =TEXT-危险})@ Html.ValidationMessageFor(型号=> model.Longitude,新{@class =TEXT-危险\r
  })@ Html.TextBoxFor(型号=> model.ClassDate,{0:MM / DD / YYYY},新{@class =的DateField形式控制})@ Html.ValidationMessageFor(型号=>模型.ClassDate,新{@class =TEXT-危险})@ Html.Hidden(纬度,Model.Latitude)@ Html.Hidden(经度\r
  Model.Longitude)\r
< / DIV>\r
<按钮式=提交ID =submitSearch级=BTN BTN-默认>搜索和LT; /按钮>\r
}

\r

\r
\r


解决方案

首先 $(本).submit()是不是指你的表单。你里面有地理codeR 相关联,所以你去打电话的 .submit()函数另一个函数地理codeR 不存在。

即使您保存表单元素给一个变量,它会造成死循环。里面的那个函数,你首先取消提交。然后您的通话功能一而再,再取消它,再次调用该函数,再取消,等等等等,直到浏览器吐假。

相反,先执行你的逻辑,那么如果你想prevent形式提交,取消它(如果你不取消它,它会做一个正常的提交)

  $(形式)。递交(函数(){
  VAR地理codeR =新google.maps.Geo codeR();
  VAR地址= $('#SEARCHQUERY)VAL()。
  VAR caSubmit = TRUE; //假设我们可以
  地理coder.geo code({'地址':地址},功能(结果状态){
    如果(状态!= google.maps.Geo coderStatus.OK){
      canSubmit = FALSE; //信号,我们不应该提交
      警报(地理code不成功,原因如下:+状态);
    }
  });
  返回canSubmit; //如果它的虚假,提交将被取消
});

Why isn't the call

this.submit()

not triggering a submit and calling my controller? The dev tools in Chrome says there is an error and that the function doesn't exist!

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

解决方案

Firstly $(this).submit() is not referring to your form. Your inside another function associated with geocoder so your trying to call the .submit() function of geocoder which does not exist.

Even if you saved the form element to a variable, it would create an endless loop. Inside that function you first cancel the submit. Then your call the function again, cancel it again, call the function again, cancel it again, and so on and so on until the browser spits the dummy.

Instead, perform your logic first, then if you want to prevent the form submitting, cancel it (and if you don't cancel it, it will do a normal 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
});

这篇关于this.submit不触发GET调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆