最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set jQuery datepicker minDate depending on input - Stack Overflow

programmeradmin0浏览0评论

I'm trying to make a jQuery datepicker set minDate depending on the value of a radio button.

When I try to write a function returning a string, my JavaScript keeps crashing. Any ideas on why this is not working?

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: "-35:+0",
    dateFormat: "yy-mm-dd",
    minDate: function() {
        var str = "-35Y";
        if($("#inputfield").val() == "value") {
            str = "-33Y";
        }
        return str;
    }
});

I'm trying to make a jQuery datepicker set minDate depending on the value of a radio button.

When I try to write a function returning a string, my JavaScript keeps crashing. Any ideas on why this is not working?

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: "-35:+0",
    dateFormat: "yy-mm-dd",
    minDate: function() {
        var str = "-35Y";
        if($("#inputfield").val() == "value") {
            str = "-33Y";
        }
        return str;
    }
});
Share Improve this question edited Mar 27, 2013 at 10:32 Simon Adcock 3,5623 gold badges28 silver badges42 bronze badges asked Mar 27, 2013 at 10:04 mattiasmattias 231 gold badge1 silver badge4 bronze badges 2
  • My javascript keeps crashing. What error are you getting? – Rob Commented Mar 27, 2013 at 10:05
  • @Rob I get: TypeError: date.getTime is not a function – mattias Commented Mar 27, 2013 at 10:12
Add a ment  | 

3 Answers 3

Reset to default 3

jsFiddle

This works fine.... you need to make sure that you have inputfield with a value

 <input type='text' id='datepicker'></div>
 <input type='text' value='value' id='inputfield'>

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: "-35:+0",
    dateFormat: "yy-mm-dd",
    minDate:mdate()
});


function mdate(){
        var str = "-35Y";
        if($("#inputfield").val() == "value"){
            str = "-33Y";
        }
        return str;
}
  • Edit:*

    $('#inputfield').on('change',function(){ $('#datepicker').datepicker('option', 'minDate', mdate()); });

The option minDate expects a value, not a function. A datepicker will not automagically update the value of minDate whenever the selection of your radio button changes.

If you want the datepicker's minimum date to change whenever the radio button selection changes, then you must explicitly program this. Something along the following lines:

$('[name=myradiogroup]').change(function () {
    var newMininumDate = /*insert logic here*/;
    $('#datepicker').datepicker('option', 'minDate', newMininumDate);
});

See http://api.jqueryui./datepicker/#method-option

This works for me:

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: "-35:+0",
    dateFormat: "yy-mm-dd",
    minDate: $("#inputfield").val()
});
发布评论

评论列表(0)

  1. 暂无评论