2016-09-07 5 views
-1

私は日付ピッカーから値を取得しようとするこのjQueryのを持っている:要素への参照が認識されないのはなぜですか?

$(document).ready(function() { 
    $("#btnGetData").click(function() { 
     document.body.style.cursor = 'wait'; 
     $.ajax({ 
      var _begdate = $("#datepickerFrom").val(); 
      var _enddate = $("#datepickerTo").val(); 

      type: 'GET', 
      url: '@Url.RouteUrl(routeName: "QuadrantData", routeValues: new { httpRoute = true, unit = "ABUELOS", begdate = "bd", enddate = "ed" })' 
       .replace("bd", encodeURIComponent(_begdate)) 
       .replace("ed", encodeURIComponent(_enddate)); 
      contentType: 'text/plain', 
      cache: false, 
      xhrFields: { 
       withCredentials: false 
      }, 
      success: function (returneddata) { 
       $("body").html(returneddata); 
      }, 
      error: function() { 
       console.log('hey, boo-boo!'); 
      } 
     }); // ajax 
     document.body.style.cursor = 'pointer'; 
    }); // button click 
}); // ready 

...しかし、それは誤り「キャッチされないでSyntaxError:予期しない識別子」スロー_begdateへの割り当てにします。

なぜですか?ここでは日付ピッカーと宣言htmlです:

<input class="smalldatepicker" type="date" id="datepickerFrom" name="daterangefrom" value="2016-08-14"> 

そのidは「datepickerFrom」であるが - なぜ失敗するvar _begdateにその値をassigしようとしていますか?

+2

あなたはあなたのAJAXパラメータのオブジェクトの宣言の中にあなたの変数の定義を置きます。これがエラーを引き起こす原因です。 –

+2

ひらめき呼び出しを使用して '()'でラップして、エンジンの衝突時にスコープが確実に保持されるようにすることが最善の方法です。 - > '@(Url.RouteUrl(routeName:" QuadrantData "、routeValues:new {httpRoute = true、unit =" GRAMPS "、begdate =" bd "、enddate =" ed "}))' –

+0

まず、AJAXコールバック内でそれらの参照が赤色に変わったことがわかりました。これは、それらが認識されていないことを示しています。私が間違っている? –

答えて

1

$.ajaxコールに渡されるオブジェクト内に変数定義を配置しています。

それはこのようなものでなければなりません:

$(document).ready(function() { 
    $("#btnGetData").click(function() { 
     document.body.style.cursor = 'wait'; 

     var _begdate = $("#datepickerFrom").val(); 
     var _enddate = $("#datepickerTo").val(); 

     $.ajax({  
      type: 'GET', 
      url: '@Url.RouteUrl(routeName: "QuadrantData", routeValues: new { httpRoute = true, unit = "ABUELOS", begdate = "bd", enddate = "ed" })' 
       .replace("bd", encodeURIComponent(_begdate)) 
       .replace("ed", encodeURIComponent(_enddate)); 
      contentType: 'text/plain', 
      cache: false, 
      xhrFields: { 
       withCredentials: false 
      }, 
      success: function (returneddata) { 
       $("body").html(returneddata); 
      }, 
      error: function() { 
       console.log('hey, boo-boo!'); 
      } 
     }); // ajax 
     document.body.style.cursor = 'pointer'; 
    }); // button click 
}); // ready 
関連する問題