使用jQuery Datepicker插件更新带有工作日值的隐藏输入元素

时间:2013-07-23 17:09:29

标签: javascript jquery

我对jQuery不太熟悉,无法让它工作。使用jQuery Datepicker插件,并且已经有了一些帮助,除了还使用所选的工作日值更新隐藏的输入元素之外,我还可以使用它。我尝试的所有内容都会干扰Datepicker插件的功能。任何帮助都会受到欢迎。

<html>
<head>

<script src="../js/jquery.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<title> jquery date picker </title>

<script type="text/javascript">
$(function () {
  $("#Datepicker").datepicker();
});
</script>

<script type="text/javascript">
function pickdate() {
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
    var selecteddate = $("#Datepicker").datepicker('getDate');
    $('#Datepickerday').html(days[selecteddate.getDay()]);

    // also needs to update hidden input 'weekday' with day of week

}
</script>

</head>
<body>

Date:&nbsp;&nbsp;<input type="text" name="Datepicker" id="Datepicker" class="Datepicker" onchange="pickdate();">
<br><br>
Day:&nbsp;<span id="Datepickerday"></span>

<input type="hidden" name="weekday" id="weekday">

</body>
</html>

1 个答案:

答案 0 :(得分:1)

使用.val()填写隐藏的输入。

function pickdate() {
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
    var selecteddate = $("#Datepicker").datepicker('getDate');
    var weekday = days[selecteddate.getDay()];
    $('#Datepickerday').html(weekday);
    $('#weekday').val(weekday);

}

或使用datepicker的选项:

$("#Datepicker").datepicker( {
    altField: 'weekday',
    altFormat: 'DD',
    onSelect: function(datestring, dp) {
        var selecteddate = dp('getDate');
        $("#Datepickerday").text($.datepicker.formatDate(selecteddate, 'DD'));
    }
});