如何为多个日期范围选择器创建Ajax发布

时间:2019-03-22 11:37:59

标签: javascript jquery ajax forms datepicker

我有一个从1到10年的日期范围(开始和结束日期)的下拉列表。 因此,如果我从下拉列表中选择3,则使用JS for循环创建3个日期范围选择器。 我有一个提交按钮,其中一个开始日期和结束日期的多个日期范围选择器都处于表单中。 我希望能够提交日期范围并将Ajax发布为JSON数据,以便可以从Python Flask中获取。

有人知道我该怎么做吗?任何帮助是极大的赞赏。谢谢!

这是我的HTML / JS代码。

filters1.html

<!DOCTYPE html>
<html>
<head>
<title>Test Filters 1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">


<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script   src="http://code.jquery.com/jquery-3.3.1.min.js"   integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="   crossorigin="anonymous"></script>
<script   src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"   integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="   crossorigin="anonymous"></script>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>



<style>

#loop-list{ 
list-style-type: none;
    }

.jumbotron{

    border-radius: 10px;
}

#single-date{
list-style-type:none;
}

</style>
</head>
<body>

<center>

<div class="jumbotron">


<h1>Date Range (yyyy-mm-dd)</h1>

<form method = "POST" action="{{url_for('test_filters1')}}">
<select>


    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>    
    <option value="7">7</option>    
    <option value="8">8</option>    
    <option value="9">9</option> 
    <option value="10">10</option>
</select>
</div>
<input type="submit" name="Submit" class="grab-dates">


</form>

<br><br>
<center>
<ul id="single-date">
<li>Date 1 <input type="date" name="start" max="20170-10-01"><i class="fa fa-calendar">
</i> To <input type="date" name="end"
min = "2021-12-31"><i class="fa fa-calendar"></i></li>

</ul>
</center>
<br>

<ul id="loop-list"></ul>

</center>

</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script   src="http://code.jquery.com/jquery-3.3.1.min.js"   integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="   crossorigin="anonymous"></script>
<script   src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"   integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="   crossorigin="anonymous"></script>




<script type="text/javascript">

var res = document.querySelector("p");
var select = document.querySelector("select");

var length;

var loopList = document.getElementById("loop-list");



select.addEventListener("change", function(e) {


  length = parseInt(select.value);
  loopList.innerHTML = "";

  if (length != 0) {
    for (var i = 1; i < length; i++) {
      loopList.innerHTML += "<li>Date " + (i + 1) +
        " <input type='date' name='start' class='start-date'" +(i+1) 
        +" max='2017-10-01'><i class='fa fa-calendar'></i> To <input type='date' name='end' class='end-date'" +(i+1)
        +" min='2021-12-31'><i class='fa fa-calendar'></i></li><br>";

    }
  }

});




$(document).on("click", ".grab-dates", function(){

    var info = $(this).parent().siblings('.start-date').text();


    alert(info);


    console.log(info);
});




</script>





</body>
</html>

关于我将与Ajax一起使用的一些想法是这样的。

var server = "http://127.0.0.1:5000";
var appdir = "/grab_dates";

$.ajax({

    type:"POST",
    url: server+ appdir,
    data: JSON.stringify(date_list),
    dataType: "json",
    contentType: "application/json;charset=UTF-8",
    success: function(data){

        console.log(data);
        console.log("Checking...");
    }
});

1 个答案:

答案 0 :(得分:1)

运行一个each函数,并将所有元素值推送到一个数组中,然后可以将其发送到ajax函数中。

简单的工作示例

$(document).on("click", ".grab-dates", function (e) {

    e.preventDefault();

    // var info = $(this).parent().siblings('.start-date').text();

    var dateArray = [];

    $('.start-date').each(function () {

        var startDate = $(this).val();
        var endDate = $(this).parent().find('.end-date').val();

        dateArray.push({ 'startDate': startDate, 'endDate': endDate });

    });

    console.log(dateArray);

    return false;

});

请参阅我更新的HTML文件。

<!DOCTYPE html>
<html>

<head>
    <title>Test Filters 1</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="icon" href="/static/images/neptune-icon2.png" type="image/png" sizes="16x16">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="icon" href="/static/images/neptune-icon2.png" type="image/png" sizes="16x16">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
        integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>


    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>



    <style>
        #loop-list {
            list-style-type: none;
        }

        .jumbotron {

            border-radius: 10px;
        }

        #single-date {
            list-style-type: none;
        }
    </style>
</head>

<body>

    <center>

        <div class="jumbotron">


            <h1>Date Range (yyyy-mm-dd)</h1>

            <form method="POST" action="{{url_for('test_filters1')}}">
                <select>


                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                </select>
        </div>
        <input type="submit" name="Submit" class="grab-dates">


        </form>

        <br><br>
        <center>
            <ul id="single-date">
                <li>Date 1 <input type="date" class="start-date" name="start" max="20170-10-01"><i
                        class="fa fa-calendar">
                    </i> To <input type="date" class="end-date" name="end" min="2021-12-31"><i
                        class="fa fa-calendar"></i></li>

            </ul>
        </center>
        <br>

        <ul id="loop-list"></ul>

    </center>

    </div>



    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
        integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>




    <script type="text/javascript">

        var res = document.querySelector("p");
        var select = document.querySelector("select");

        var length;

        var loopList = document.getElementById("loop-list");



        select.addEventListener("change", function (e) {


            length = parseInt(select.value);
            loopList.innerHTML = "";

            if (length != 0) {
                for (var i = 1; i < length; i++) {
                    loopList.innerHTML += "<li>Date " + (i + 1) +
                        " <input type='date' name='start' class='start-date'" + (i + 1)
                        + " max='2017-10-01'><i class='fa fa-calendar'></i> To <input type='date' name='end' class='end-date'" + (i + 1)
                        + " min='2021-12-31'><i class='fa fa-calendar'></i></li><br>";

                }
            }

        });




        $(document).on("click", ".grab-dates", function (e) {

            e.preventDefault();

            // var info = $(this).parent().siblings('.start-date').text();

            var dateArray = [];

            $('.start-date').each(function () {

                var startDate = $(this).val();
                var endDate = $(this).parent().find('.end-date').val();

                dateArray.push({ 'startDate': startDate, 'endDate': endDate });

            });

            console.log(dateArray);

            // var server = "http://127.0.0.1:5000";
            // var appdir = "/grab_dates";

            // $.ajax({

            //     type:"POST",
            //     url: server+ appdir,
            //     data: JSON.stringify(dateArray),
            //     dataType: "json",
            //     contentType: "application/json;charset=UTF-8",
            //     success: function(data){

            //         console.log(data);
            //         console.log("Checking...");
            //     }
            // });

            return false;

        });




    </script>





</body>

</html>