2014-02-16 75 views
0

我正在查找用戶輸入的地址的經度和緯度。我正在調用一個表單按鈕點擊的jQuery函數。但是控制器方法在這個jQuery函數之前執行。我想要的是,當用戶點擊提交按鈕時,在調用成功控制器函數之後調用jQuery函數。jQuery onclick函數未執行

查看:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">   </script> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
     function getCoordinates() { 
      $("#sumbitBtn").click(function(){ 
      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ 'address': '#street' }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        alert("location : " + results[0].geometry.location.lat() + " " + results[0].geometry.location.lng()); 
        $(selector).val(value) 
        var latitude = results[0].geometry.location.lat(); 
        var longitude = results[0].geometry.location.lng(); 
        $('latitude').val('latitude'); 
        $('longitude').val('longitude'); 

        $.ajax({ 
         type: "POST", 
         dataType: 'json', 
         data: { 

          mylat: latitude, 
          mylang: longitude 
         }, 
         url: "RentOutSpace/AddSpaces", 
         success: function (result) { success(result); } 
        }); 

       } else { 
        alert("Something got wrong " + status); 
       } 
      }); 
      }); 
    } 

@using (Html.BeginForm("AddSpace","RentOutSpace","$.post()")) 
{ 


@Html.ValidationSummary(true, "Please correct the errors and try again") 

    @Html.EditorFor(model => model.latitude) 
    @Html.EditorFor(model => model.longitude) 
     <div class="editor-label"> 
     @Html.LabelFor(model => model.street) 
     </div> 
     <div class="editor-field"> 
     @Html.TextBoxFor(model => model.street) 
     @Html.ValidationMessageFor(model => model.street) 
     </div> 

    <input type="submit" value="submit" onclick="getCoordinates()" id="sumbitBtn"/> 
} 

控制器:

  [HttpPost] 
      public ActionResult AddSpace(RentOutSpace rentModel, string lati, string lang) 
      { 
      rentModel.latitude = lati; 
      rentModel.longitude = lang; 


     Session.Store(rentModel); 
     Session.SaveChanges(); 
     return View(); 
     } 

回答

2

只是讀你的代碼似乎它的執行。你剛纔設置的另一個事件處理程序:

function getCoordinates() { 
    $("#sumbitBtn").click(function(){ 
     var geocoder = new google.maps.Geocoder(); 
     ... 
} 

我認爲你必須刪除了這一行

$("#sumbitBtn").click(function(){ 

和關閉});得到它的工作。

更新:這是最小的測試顯示在你的代碼錯誤:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 

<script type="text/javascript"> 
function getCoordinates() { 
    console.log('onsubmit called!'); 
    $("#sumbitBtn").click(function(){ 

     console.log('$("#sumbitBtn").click() deleted!'); 
    }); 
} 
</script> 
</head> 

<body> 
    <input type="submit" value="submit" onclick="getCoordinates()" id="sumbitBtn"/> 
</body> 
</html> 

加載此代碼,並按下提交按鈕,您將獲得在控制檯窗口消息:

onsubmit called! 

要獲得它正常運行並顯示第二條消息(它不是geocode()函數,您必須刪除$("#sumbitBtn").click(function(){並關閉部分。之後,我們會得到兩條消息:

onsubmit called! 
$("#sumbitBtn").click() deleted! 

如果您的代碼在更改後不起作用意味着存在其他錯誤。

+0

我刪除了這些行但getCoordinates()函數未執行 – Wasfa

+0

什麼是$(selector).val(val ue)''在'geocode()'部分? –

+0

我將隱藏文本框的名稱設置爲經度和緯度的值,以獲得地理編碼的座標值,以便將其傳遞給控制器​​以將其保存在數據庫中。 – Wasfa

1

首先,你有你的功能:

function getCoordinates() { 
      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ 'address': '#street' }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        alert("location : " + results[0].geometry.location.lat() + " " + results[0].geometry.location.lng()); 
        $(selector).val(value) 
        var latitude = results[0].geometry.location.lat(); 
        var longitude = results[0].geometry.location.lng(); 
        $('latitude').val('latitude'); 
        $('longitude').val('longitude'); 

        $.ajax({ 
         type: "POST", 
         dataType: 'json', 
         data: { 

          mylat: latitude, 
          mylang: longitude 
         }, 
         url: "RentOutSpace/AddSpaces", 
         success: function (result) { success(result); } 
        }); 

       } else { 
        alert("Something got wrong " + status); 
       } 
      }); 

      return false;//prevent form submit 
    } 

然後你就可以將它的元素的onClick的JavaScript的方法:或者

<input type="submit" value="submit" onclick="getCoordinates()" id="sumbitBtn"/> 

在一個單獨的文件/段jquery的方式:

$(function(){//when the document is ready 
    $("#sumbitBtn").click(getCoordinates); 
}); 
+0

它沒有執行。 – Wasfa

+0

好的做它的jQuery的方式,在這裏http://jsfiddle.net/4WB8E/ – Wilmer

+0

感謝功能現在正在執行,但緯度和經度值不會發布到控制器。 – Wasfa