将年份从输入添加到 ajax 调用

我有一个页面 A,其中有一个输入年份和月份下拉列表的表单。


            <form method="post" action="" name="form_cal" id="form_cal" class="inputform">

                <div class="h5"><strong>Total Number of People Registered:</strong> <?Php echo $totalpersons; ?></div>

                <div class="clear_5"></div>

                <div>

                    <span><label class="h5"><strong>Year *</strong></label></span>

                    <div class="clear_1"></div>

                    <span><input name="year" type="text" class="textbox" id="year" required></span>

                </div>

                <div>

                    <span><label class="h5"><strong>Month for Screening</strong></label></span>

                    <span>                          

                        <select name="month" id="month" value="" tabindex="1" aria-hidden="true" required style="height:50px;display: block;padding:10px;" class="h5">

                       </select>

                    </span>

                </div>

            </form>

我还有另一个页面 B,它通过 ajax 在页面 A 上选择了月份中的所有日期。


如果我使用年份的日期功能,date("Y");它工作正常。我得到指定月份的所有日期。但是年份的日期功能仅适用于当年。我想要实现的是能够在输入字段中输入年份,当调用ajax时,将输入字段中的年份值传递给ajax,以便显示所选月份的日期指定的年份。下面是我的ajax调用(我认为这是我需要进行调整的地方。但无法找出最好的方法)


$(document).ready(function(){ 

  var timer = null; 

  var dataString; 

  function submitForm(){

      $.ajax({ type: "POST",

                url: "cim-calendar-action.php",

                data: dataString,

                success: function(result){

                    $('#calendar-display').html(result);

                }

      });

      return false;

  }


任何帮助将不胜感激


HUX布斯
浏览 141回答 2
2回答

慕神8447489

对于 javascript,我建议如下:$(document).ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; var timer = null;&nbsp; &nbsp; &nbsp; &nbsp; var dataObj = Object();&nbsp; &nbsp; &nbsp; &nbsp; function submitForm(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({ type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "cim-calendar-action.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {dateObj: JSON.stringify(dataObj)},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#calendar-display').html(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $('#month').on('change', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearTimeout(timer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var month = $(this).val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataObj.month = month;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataObj.year = $('#year').val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer = setTimeout(submitForm, 050);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });然后要从 php 获取信息,您只需像这样得到它:$dateObj = json_decode($_POST['dateObj']);echo $dateObj->month.'/'.$dateObj->year;对于日期函数,您需要传入第二个参数,其中包含您要指定的年份信息在这里阅读更多:https : //www.php.net/manual/en/function.date.php

江户川乱折腾

经过进一步阅读和测试,我能够解决它。感谢@Studocwho 的指导。并感谢其他评论并提出解决方案的人。这是我的工作解决方案:$(document).ready(function(){&nbsp;&nbsp; &nbsp; var timer = null;&nbsp;&nbsp; &nbsp; var dataString;&nbsp;&nbsp; &nbsp; function submitForm(){&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({ type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "cim-calendar-action.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: dataString,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#calendar-display').html(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; $('#month').on('change', function() {&nbsp; &nbsp; &nbsp; &nbsp; clearTimeout(timer);&nbsp; &nbsp; &nbsp; &nbsp; var month = $(this).val();&nbsp; &nbsp; &nbsp; &nbsp; var year = $("#year").val();&nbsp; &nbsp; &nbsp; &nbsp; dataString = {'month': month, 'year': year };&nbsp; &nbsp; &nbsp; &nbsp; timer = setTimeout(submitForm, 050);&nbsp; &nbsp; });});我添加var year = $("#year").val();并将数据字符串更改为dataString = {'month': month, 'year': year };
打开App,查看更多内容
随时随地看视频慕课网APP