从HTML datepicker获取日期并发送到JS脚本

时间:2016-06-30 08:04:54

标签: javascript php html datepicker

我想将HTML表单中设置的日期和时间发送到js脚本(不同的页面)。但是,我是一个菜鸟,不知道xD是怎么回事 我到目前为止所尝试的是将日期和时间(从两个输入)发送到同一页面,触发PHP代码,设置包含两个值的cookie(这可行)。

如何获取此数据并将其设置为js函数?

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

先谢谢你们!

2 个答案:

答案 0 :(得分:1)

使用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();
    });

});

答案 1 :(得分:0)

有更好的方法来做到这一点。一种简单而无差错的方式。使用Angular JS。这类任务有很多预先构建的代码。你做过Angular吗?而且你也可以使用这个日期选择器。

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

它是一个伟大的,非常定制的。而且你也可以使用MVC结构。

相关问题