Javascript 选择使用 jquery datepicker 选择的日期的周数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12515448/
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-08-23 10:35:08  来源:igfitidea点击:

Select the week number of the date picked using jquery datepicker

javascriptjquerydatepickerjquery-ui-datepicker

提问by Tom

I have the following code for selecting a date which I then want to convert to a week number.

我有以下代码用于选择一个日期,然后我想将其转换为周数。

    $(".calendar").datepicker({
        showWeek: true,
        onSelect: function(dateText, inst) {
            dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)),
            alert($.datepicker.iso8601Week(new Date(dateText)))
        }
    });

The alert shows the correct week number, but the date does not reformat at all.

警报显示正确的周数,但日期根本没有重新格式化。

I can't move the dateFormat function outside of the onSelect because this causes nothing to happen.

我无法将 dateFormat 函数移到 onSelect 之外,因为这不会导致任何事情发生。

What I would like to achieve is the text field to say something like "Week Number 13"

我想要实现的是文本字段说“第 13 周”之类的内容

Any ideas?

有任何想法吗?

http://jsfiddle.net/ENG66/

http://jsfiddle.net/ENG66/

回答by James Wiseman

I've updated the fiddle to make it work: http://jsfiddle.net/ENG66/11/

我已经更新了小提琴以使其工作:http: //jsfiddle.net/ENG66/11/

In the onselect event, you need to use .val() to override the setting of the textbox value

在 onselect 事件中,需要使用 .val() 来覆盖 textbox 值的设置

$(".calendar").datepicker({
        showWeek: true,
        onSelect: function(dateText, inst) {
            $(this).val("'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)));
        }
    });?

EDIT

编辑

As Igor Shastin pointed out in his comment below we already have the text box in inst.input

正如 Igor Shastin 在下面的评论中指出的那样,我们已经有了文本框 inst.input

inst.input.val($.datepicker.iso8601Week(new Date(dateText)));

回答by j08691

Try this jsFiddle example.

试试这个jsFiddle 示例

$(".calendar").datepicker({
    showWeek: true,
    onSelect: function(dateText, inst) {
        dateFormat: "'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)),
        $(this).val('Week:' + $.datepicker.iso8601Week(new Date(dateText)));
    }
});?

回答by DAB

The above answers assume that the date format can be handled natively by Javascript. In the case of short European dates (dd/mm/yyyy, as opposed to the American mm/dd/yyyy), you can get the date value via datepicker itself, which will handle the conversion automatically.

上面的答案假设日期格式可以由 Javascript 本地处理。在欧洲日期较短的情况下(dd/mm/yyyy,而不是美国的 mm/dd/yyyy),您可以通过 datepicker 本身获取日期值,它会自动处理转换。

$("#week_field").val( $.datepicker.iso8601Week( $("#date_field").datepicker( "getDate" ) ));

You can set a different date format for datepicker like this

您可以像这样为 datepicker 设置不同的日期格式

$.datepicker.setDefaults( {dateFormat : "dd/mm/yy"} );