javascript foo.substr 给出“不是函数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17735717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:25:51  来源:igfitidea点击:

foo.substr gives "Is not a function" error

javascriptjquery

提问by Clay

I'm using Jquery's datepicker plugin (which works fine). Then I need to extract the "day of the week" from the picked date.

我正在使用 Jquery 的 datepicker 插件(工作正常)。然后我需要从选择的日期中提取“星期几”。

When using foo.substring(0,3)to assign the first three characters of the datepicker('getDate')I get: TypeError foo.substr is not a function.

foo.substring(0,3)用于分配datepicker('getDate')I get的前三个字符时:TypeError foo.substr is not a function.

$(function () {
    $("#textDatepicker").datepicker();
});

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.substr(0, 3)
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="https://jquery-blog-js.googlecode.com/files/SetCase.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
</head>
<body>
Select Date:&nbsp;<input type="text" id="textDatepicker" onchange="selectedDay();">
<br><br>
<span id="dayofweek">Selected day of week replaces this</span>
</body>

I have also pasted at: jsfiddle

我也粘贴在:jsfiddle

Any help would be appreciated.. Thanks in advance...

任何帮助将不胜感激.. 提前致谢...

回答by adeneo

var foo = $("#textDatepicker").datepicker('getDate');

returns a Date object, not a string, and has no method substr()

返回一个 Date 对象,而不是一个字符串,并且没有方法 substr()

FIDDLE

小提琴

You can solve it by removing that unsightly inline event handler and doing :

您可以通过删除难看的内联事件处理程序并执行以下操作来解决它:

$("#textDatepicker").datepicker({
    onSelect: function() {
        var date = $(this).datepicker('getDate');
        var day  = $.datepicker.formatDate('DD', date);
        $('#dayofweek').html(day);
    }
});

FIDDLE

小提琴

回答by The Alpha

Use

利用

var weekday = foo.toString().substr(0, 3);

DEMO.

演示。

回答by Jonast92

$("#textDatepicker").datepicker('getDate');

is an object. You can't take the substring of an object with substr.

是一个对象。您不能使用 substr 获取对象的子字符串。

回答by sunshine

foo.substr(0,3) is the culprit. It returns a date object and you can do substr on a date object. You could fix the problem by using the code below

foo.substr(0,3) 是罪魁祸首。它返回一个日期对象,您可以对日期对象执行 substr。您可以使用以下代码解决问题

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.getDay();
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}
function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.getDay();
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

回答by Ismael ozil

called the toString();method before calling the subString();like foo.toString().subString(start,length);Worked for me

被称为toString();调用方法之前subString();一样foo.toString().subString(start,length);对我来说有效