【问题标题】:How use ajax post when datepicker change date?datepicker更改日期时如何使用ajax post?
【发布时间】:2013-02-06 09:01:14
【问题描述】:

下午好

使用 datepicker ui 更改输入中的值。

我想在输入值发生变化时使用 ajax post。

为此我使用脚本:

<input type="text" name="date" class="datepicker" id="datepicker">

$(".datepicker").datepicker({changeYear: true});

$(".datepicker").on("change",function(){
var date=$(this).val();
$.post("test.php", {
date:date
},
function(data){$('#testdiv').html('');$('#testdiv').html(data);
});
});

但是ajax post查询是用旧日期执行的。

日期选择器在输入中没有时间变化值。

告诉我datepicker更改字段日期后如何查询?

我需要:

1) datepicker 更改日期;

2) 应使用新日期发送查询。

【问题讨论】:

    标签: ajax jquery datepicker


    【解决方案1】:

    您可以在 datepicker 事件上触发 ajax 发布:

    $("#datepicker").datepicker({
    
        onSelect: function(date, instance) {
    
            $.ajax
            ({
                  type: "Post",
                  url: "www.example.com",
                  data: "date="+date,
                  success: function(result)
                  {
                      //do something
                  }
             });  
         }
    });
    

    希望这会有所帮助-@Codebrain

    【讨论】:

      【解决方案2】:

      也许您应该考虑使用日期选择器的onSelect 事件(请参阅here)。

      使用该事件,新选择的日期(作为字符串)作为第一个参数传递。

      【讨论】:

        【解决方案3】:

        你可以使用changeDate

        $('.datepicker').datepicker()
         .on('changeDate', function(e) {
            // `e` here contains the extra attributes
        });
        

        你可以在这里查看:http://bootstrap-datepicker.readthedocs.io/en/latest/events.html

        【讨论】:

          【解决方案4】:

          您使用onSelect 代替on(change 用于日期选择器,它会在日期更改时触发。

          $(function(){
              $('.datepicker').datepicker({
                  onSelect:function(dateText,instance){
                      alert(dateText); //Latest selected date will give the alert.
                      $.post("test.php", {
                      date:dateText // now you will get the selected date to `date` in your post
                      },
                      function(data){$('#testdiv').html('');$('#testdiv').html(data);
                      });
                  }
              });
          });
          

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-18
            相关资源
            最近更新 更多