简体   繁体   English

如何在jQuery更改中获取日期选择器的值

[英]How to get the value of date picker on change in jquery

I need to get the value of date onchange , 我需要获取onchange日期的值,

Here is the code 这是代码

<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>

Here is my jquery 这是我的jQuery

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

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

Onchange the function is not calling, I have just create alert on change but its not working Onchange函数未调用,我只是在更改时创建警报,但它不起作用

try this ; 尝试这个 ;

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

Remove the .datepicker() if it's done earlier 删除.datepicker()如果已完成)

it is better to use jquery built in functions inside a jquery context . 最好是使用jquery内置函数内部jquery context Also your jquery change function syntax and javascript code to get the value from date picker is wrong. 另外,从日期选择器获取值的jquery change function语法和javascript代码是错误的。

change your jquery change function to: 将您的jquery change函数更改为:

$(document).ready(function(){

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

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

    });

});

Make sure you wrapped the change function with document ready fn 确保用文档就绪fn包装了更改功能

you can easily do with below code: 您可以轻松地使用以下代码:

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

    alert($('#value_date').val());
}
</script>

Notice the two changes - remove get by id and changed to .val(). 注意这两个更改-删除get by id并更改为.val()。

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

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