1

我要做的就是使用 5 个文本框、一个日历和一个按钮在日期选择器日历上创建一个事件。

这是格式

在此处输入图像描述

我已阅读有关 .setDate() 和 .getDate() 的所有信息,但我无法让它们工作。我没有插件经验,只有简单的 html 和 javascript。什么是 datepicker 对象,为什么每个人的代码都没有为其功能命名?

任何有此插件经验的人都应该能够在 5 秒内完成此操作

这是我的代码

<!DOCTYPE html>
<html>
<head>

  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>jQuery &amp; jQueryUI Base - jsFiddle demo</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.5.js'></script>
  <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  <link rel="stylesheet" type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.7/themes/black-tie/jquery-ui.css">
  <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script>
  <style type='text/css'>
    table.ui-datepicker-calendar tbody td.highlight > a {
    background: url("images/ui-bg_inset-hard_55_ffeb80_1x100.png") repeat-x scroll 50% bottom #FFEB80;
    color: #363636;
    border: 1px solid #FFDE2E;
}

  </style>



<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
var equip = document.getElementById('equipment').value;
var size = document.getElementById('size').value;
var surface = document.getElementById('surface').value;
var orderNumber = document.getElementById('orderNumber').value;
var responsible = document.getElementById('responsible').value;


var events = [ 
    { Title: "Equipment: " + equip + "\nSize: " + size + dated +
    "\nRequired on Surface: " + surface + "\nWork Order Number: " + orderNumber + "\nResponsible: " + responsible, Date: new Date("05/13/2013") }, 
    { Title: "Dinner", Date: new Date("02/25/2011") }, 
    { Title: "Meeting with manager", Date: new Date("03/01/2011") }
];


$("div").datepicker({

    beforeShowDay: function(date) {
        var result = [true, '', null];
        var matching = $.grep(events, function(event) {
            return event.Date.valueOf() === date.valueOf();
        });

        if (matching.length) {
            result = [true, 'highlight', null];
        }
        return result;
    },
    onSelect: function(dateText) {
        var date,
            selectedDate = new Date(dateText),
            i = 0,
            event = null;

        while (i < events.length && !event) {
            date = events[i].Date;

            if (selectedDate.valueOf() === date.valueOf()) {
                event = events[i];
            }
            i++;
        }
        if (event) {
            alert(event.Title);
        }
    }
});

});//]]>



var dated = $( "div.selector" ).datepicker( "getDate" );

function alerter(form) {alert (form.size.value)}
function dog () {div.setDate("+2d");}


function submit(form){
var equip1 = form.equipment.value;
var size1 = form.size.value;
var surface1 = form.surface.value;
var orderNumber1 = form.orderNumber.value;
var responsible1 = form.responsible.value;
var inputDate1 = form.inputDate.value

var events = [ 
    { Title: "Equipment: " + equip1 + "\nSize: " + size1  +
    "\nRequired on Surface: " + surface1 + "\nWork Order Number: " + orderNumber1 + "\nResponsible: " + responsible1, Date: new Date(inputDate1) }, 
    { Title: "Dinner", Date: new Date("05/25/2013") }, 
    { Title: "Meeting with manager", Date: new Date("03/01/2011") }
];
}
</script>


</head>
<body>
  <FORM>
    Equipment: <input type='text' id='equipment' /> <br />
    Size: <input type='text' id='size' /> <br />
    Required on Surface: <input type='checkbox' id='surface' /> <br />
    Work Order Number: <input type='text' id='orderNumber' /> <br />
    Responsible: <input type='text' id='responsible' /> <br />

    <div id="datepicker"></div>
    <button type="button" onclick="alerter(this.form)">Add Lowering Event</button><br>
   </FORM>
</body>


</html>

编辑代码:

<!DOCTYPE html>
<html>
<head>

  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>jQuery &amp; jQueryUI Base - jsFiddle demo</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.5.js'></script>
  <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  <link rel="stylesheet" type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.7/themes/black-tie/jquery-ui.css">
  <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script>
  <style type='text/css'>
    table.ui-datepicker-calendar tbody td.highlight > a {
    background: url("images/ui-bg_inset-hard_55_ffeb80_1x100.png") repeat-x scroll 50% bottom #FFEB80;
    color: #363636;
    border: 1px solid #FFDE2E;
}

  </style>



<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
var equip = document.getElementById('equipment').value;
var size = document.getElementById('size').value;
var surface = document.getElementById('surface').value;
var orderNumber = document.getElementById('orderNumber').value;
var responsible = document.getElementById('responsible').value;
var date = document.getElementById('dateds').value

var events = [ 
    { Title: "Equipment: " + equip + "\nSize: " + size + date +
    "\nRequired on Surface: " + surface + "\nWork Order Number: " + orderNumber + "\nResponsible: " + responsible, Date: new Date(date) }, 
    { Title: "Dinner", Date: new Date("02/25/2011") }, 
    { Title: "Meeting with manager", Date: new Date("03/01/2011") }
];


$("#datepicker").datepicker({

    beforeShowDay: function(date) {
        var result = [true, '', null];
        var matching = $.grep(events, function(event) {
            return event.Date.valueOf() === date.valueOf();
        });

        if (matching.length) {
            result = [true, 'highlight', null];
        }
        return result;
    },
    onSelect: function(dateText) {
        var date,
            selectedDate = new Date(dateText),
            i = 0,
            event = null;

        while (i < events.length && !event) {
            date = events[i].Date;

            if (selectedDate.valueOf() === date.valueOf()) {
                event = events[i];
            }
            i++;
        }
        if (event) {
            alert(event.Title);
        }
    }
});

});//]]>



var dated = $("#datepicker").datepicker( "getDate" );

function alerter() {alert (dated)}
function dog () {div.setDate("+2d");}


function submit(form){
var equip1 = form.equipment.value;
var size1 = form.size.value;
var surface1 = form.surface.value;
var orderNumber1 = form.orderNumber.value;
var responsible1 = form.responsible.value;
var inputDate1 = form.inputDate.value

var events = [ 
    { Title: "Equipment: " + equip1 + "\nSize: " + size1  +
    "\nRequired on Surface: " + surface1 + "\nWork Order Number: " + orderNumber1 + "\nResponsible: " + responsible1, Date: new Date(inputDate1) }, 
    { Title: "Dinner", Date: new Date("05/25/2013") }, 
    { Title: "Meeting with manager", Date: new Date("03/01/2011") }
];
}
</script>


</head>
<body>
  <FORM>
    Equipment: <input type='text' id='equipment' /> <br />
    Size: <input type='text' id='size' /> <br />
    Required on Surface: <input type='checkbox' id='surface' /> <br />
    Work Order Number: <input type='text' id='orderNumber' /> <br />
    Responsible: <input type='text' id='responsible' /> <br />
    Date: <input type="text" id="dateds" /></p>

    <div id="datepicker"></div>
    <button type="button" onclick="alerter()">Add Lowering Event</button><br>
   </FORM>
</body>


</html>
4

1 回答 1

1

$("div").datepicker并且$("div.selector").datepicker都应该是$("#datepicker").datepicker。第一个会将日期选择器附加到页面上的每个 DIV,第二个不起作用,因为您没有带有 class 的 DIV selector

通常,日期选择器附加到<input>表单中的元素,因此当您提交表单时,将提交所选日期。由于您将其放在 DIV 中,因此您可以<input type="hidden">在表单中添加一个元素:

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

并将以下选项添加到日期选择器:

altField: "date",

使用命名函数与匿名函数主要是一种风格选择。如果短函数只用在一个地方,例如onSelect选项或 AJAX 回调,通常将其作为匿名函数内联。

于 2013-05-09T17:22:35.787 回答