简体   繁体   English

带有jQuery datepicker的Ajax发布未显示日期

[英]Ajax post with jQuery datepicker not showing date

I have a datepicker in my form and I would like to post the form data with an ajax call. 我的表单中有一个日期选择器,我想用ajax调用发布表单数据。 My problem is that the form does not get the date selected from the datepicker. 我的问题是该表单无法获取从日期选择器中选择的日期。

Here is the link to the theme I am using with the datepicker: http://wrapbootstrap.com/preview/WB00U99JJ 以下是指向日期选择器使用的主题的链接: http ://wrapbootstrap.com/preview/WB00U99JJ

Here is the html for the datepicker which is inside the form: 这是表单中datepicker的html:

<div id="datepicker-inline" name="date"></div>

Here is my ajax post 这是我的ajax帖子

$('#submit').on('click', function() {
    var form = $('form');
    var date = $('#datepicker-inline');
    $.ajax('/api/service', {
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: form.serializeArray(),
        success: function(response) {
            console.log("Success!");
            $('#status').addClass('status-bar-success');
        },
        error: function() {
            console.log('OH NOES!');
            console.log(form.serializeArray());
            console.log(date.val());
            $('#status').addClass('status-bar-error');
        }
    });
});

I am running in the console to see what I get. 我在控制台中运行以查看得到的结果。 The form.serializeArray() is working and the date.val() gets the date selected. form.serializeArray()正在工作,date.val()获取选定的日期。 My quesion is how do I had the date value into the form.serializeArray()? 我的问题是如何将日期值放入form.serializeArray()?

* UPDATE * *更新*

html html

<div id="datepicker-inline"></div>
<input type="text" id="date_hidden" name="date added" class="no_show"/>

ajax 阿贾克斯

$('#submit').on('click', function() {
    var form = $('form');
    var date = $('#datepicker-inline').val();
    $('#date_hidden').val(date);
    $.ajax('/api/service', {
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: form.serializeArray(),
        success: function(response) {
            console.log("Success!");
            $('#status').addClass('status-bar-success');
        },
        error: function() {
            console.log('OH NOES!');
            console.log(form.serializeArray());
            console.log(date);
            $('#status').addClass('status-bar-error');
        }
    });
});

You could have a hidden element alongside that div since it doesn't have any input element. 您可以在该div旁边有一个hidden元素,因为它没有任何input元素。

<div id="datepicker-inline" name="date"></div>
<input type="hidden" id="datepicker-inline_hidden" name="date"/>

And in your submit you could do 在提交中,您可以做

$('#submit').on('click', function() {
    var form = $('form');
    var date = $('#datepicker-inline');
    $('#datepicker-inline_hidden').val(date);
    //... your code

So that your date value is submitted as the hidden element. 这样您的日期值将作为hidden元素提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM