datepicker简单事件创建

时间:2013-05-09 14:39:10

标签: javascript jquery html datepicker

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

这是格式

enter image description here

我已经阅读了有关.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>

1 个答案:

答案 0 :(得分:1)

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

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

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

并将以下选项添加到datepicker:

altField: "date",

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