【问题标题】:How to get the value of date picker on change in jquery如何在jquery中获取日期选择器的值
【发布时间】:2016-07-19 06:25:22
【问题描述】:

我需要获取日期 onchange 的值,

这里是代码

<div class="input-daterange" data-date-format="dd/mm/yyyy">

       <label>Date </label>                                                       
<input id="value_date"  class="form-control"  name="start" type="text" />                                                                              
</div>

这是我的 jquery

$('#value_date').change(function() {{

    var valuefirstone = document.getElementById('#value_date').value;
    alert(valuefirstone);
}
</script>

Onchange 函数没有调用,我刚刚创建了更改警报,但它不起作用

【问题讨论】:

  • 删除你的 getElementById 上的“#”
  • 使用简单的 $('#value_date').val();
  • 你为什么不使用 $(this).val() ?
  • 函数没有调用onchange

标签: javascript jquery datepicker


【解决方案1】:

试试这个;

$('#value_date').datepicker().on('changeDate', function (ev) {
    var valuefirstone = $(this).val();
    alert(valuefirstone);
});

删除.datepicker()(如果之前已完成)

【讨论】:

  • 你在使用引导日期选择器吗?你确定它在你的页面上正确加载了吗?
  • 是的,我正在使用引导日期选择器,它已加载
【解决方案2】:

最好在jquery context 中使用jquery 内置函数。此外,您从日期选择器获取值的 jquery change function 语法和 javascript 代码也是错误的。

将您的 jquery 更改功能更改为:

$(document).ready(function(){

    $('#value_date').change(function() {

        var valuefirstone = $(this).val();//use jquery built in functions
        alert(valuefirstone);

    });

});

确保你用 document ready fn 包装了 change 函数

【讨论】:

    【解决方案3】:

    您可以使用以下代码轻松完成:

     $("#value_date").on('change', function(event) {
       event.preventDefault();
       alert(this.value);
      /* Act on the event */
      });
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多