从HTML日期选择器中获取日期并发送到JS脚本


Get date from HTML datepicker and send to JS script

我想发送的日期和时间设置在一个HTML表单的js脚本(不同的页面)。然而,我是一个新手,不知道如何xD到目前为止,我所尝试的是将日期和时间(从两个输入)发送到同一页面,触发PHP代码,设置包含这两个值的cookie(这有效)。

我该如何得到这些数据并将其设置为js函数?

日期和时间格式是否正确?如果没有,我该如何解决这个问题?

提前感谢各位!

建议使用jquery和ajax post方法。

例如,你有一个表单:

<form action="process.php" method="post">
 <input class="date" type="date" name="date">
 <button type="submit value="submit" name="submit">
</form>
Jquery代码:

$(document).ready(function() {
    // process the form
    $('form').submit(function(event) {
        // get the form data
        // there are many ways to get this data using jQuery (you can use the class or id also)
        var formData = {
            'date'              : $('input[name=date]').val()
        };
        // process the form
        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url         : 'process.php', // the url where we want to POST
            data        : formData, // our data object
            dataType    : 'json', // what type of data do we expect back from the server
            encode      : true
        })
            // using the done promise callback
            .done(function(data) {
                // log data to the console so we can see
                console.log(data); 
                // here we will handle errors and validation messages
            });
        // stop the form from submitting the normal way and refreshing the page
        event.preventDefault();
    });
});

有更好的方法。一个简单和错误自由的方式。使用Angular JS。有很多针对这类任务的预构建代码。你用过Angular吗?你也可以用这个日期选择器。

http://xdsoft.net/jqplugins/datetimepicker/

这是一个伟大的和非常定制的。你也可以使用MVC结构