最初将当前日期作为占位符放在输入字段datepicker bootstrap中

时间:2015-01-07 09:26:59

标签: javascript jquery html twitter-bootstrap

我希望将所有字段的日期设置为当前日期,开始日期为当前日期前两年,结束日期为今日日期,在日期选择器中。但输入字段中的占位符不是当前日期



$(document).ready(function() {
		/*var $datepicker = $( "#datepicker" );
        $datepicker.datepicker();
        $datepicker.datepicker('setDate', new Date());*/
		
		$('input#datepicker').click(function() {
		  // document.getElementById('datePicker').valueAsDate = new Date();
		
            //alert($(this).attr('id'));
			   alert("you can only post interviews not more older than the last two years!!!");
           });
		
		
		// adding todays date as the value to the datepickers.
		
        var d = new Date();
        var curr_day = d.getDate();
        var curr_month = d.getMonth() + 1; //Months are zero based
        var curr_year = d.getFullYear();
        var eutoday = curr_day + "-" + curr_month + "-" + curr_year;
        var ustoday = curr_month + "-" + curr_day + "-" + curr_year;
        $("div.datepicker input").attr('value', eutoday);
        $("div.usdatepicker input").attr('value', ustoday);
            //document.getElementById('datePicker').value = new Date().toDateInputValue();
        //calling the datepicker for bootstrap plugin
        // https://github.com/eternicode/bootstrap-datepicker
        // http://eternicode.github.io/bootstrap-datepicker/
        $('#datepicker').datepicker({
		   // setDate: new Date(),
		//   datepicker('setDate', new Date())
			//document.getElementById('datePicker').valueAsDate = new Date();
			//document.getElementById('datePicker').value = new Date().toDateInputValue();
            autoclose: true,
			
          //  startDate: new Date()
			
    startDate: '-24m',
    // endDate: '+2d'
	endDate: new Date()

        });
    });




2 个答案:

答案 0 :(得分:1)

$( document ).ready(function() {
    var date = Date();
   $("#yourdateinput").attr("placeholder",date);
});

答案 1 :(得分:0)

你可以试试这个: -

$( document ).ready(function() {
    var date = Date();
    document.getElementById("yourInputID").value = date;
});