为什么这个元素的引用无法识别?

时间:2016-09-07 21:14:30

标签: javascript jquery html

我有这个jQuery试图从Datepicker获取值:

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

...但它会在_begdate的赋值上抛出错误“ Uncaught SyntaxError:Unexpected identifier ”。

为什么呢?这是html声明Datepicker:

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

它的id是“datepickerFrom” - 那么为什么尝试将其值赋予var _begdate失败?

1 个答案:

答案 0 :(得分: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