2014-04-27 68 views
0

这可能只是一个简单的错误,但我花了最后3个小时试图弄清楚 - 没有成功。jQuery Datepicker中的onSelect事件似乎不会触发

我最终想做的是使用输入的日期,并将其与截止日期进行比较,以表明提交是否为时过晚。但是,目前为了简化目的,我只想告诉我日期已经改变,但没有成功。我在哪里错了?

$("#id_submission_date").datepicker({ 
    onSelect: function(dateText) { 
     $("#late_submission_warning").text("Date selected"); 
    } 
}); 

下面是从页(缩短)HTML:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title> Feedback for Student, Test</title> 
    <meta name="description" content=""> 
    <link href="/static/css/bootstrap.css" rel="stylesheet" media="screen"> 
    <link href="/static/css/jquery.qtip.min.css" rel="stylesheet"> 
    <style type="text/css"> 
     body { 
      padding-top: 60px; 
      padding-bottom: 40px; 
     } 
     .sidebar-nav{ 
      padding: 9px 0; 
     } 
    </style> 
</head> 

<body> 
<nav class="navbar navbar-default navbar-fixed-top" role="navigation"> 
... 
</nav> 

<form action="" method="post" accept-charset="utf-8" role="form"> 
<input type='hidden' name='csrfmiddlewaretoken' value='...' /> 

<div class="container"> 

    <h1>European Law (2013/2014): Essay</h1> 
    <h2>Test Student</h2> 
    ... 

    <label for="submission_date">Submission Date</label> 
    <input class="form-control" data-date-format="dd/mm/yyyy" id="id_submission_date" name="submission_date" type="text" value="13/04/2015" /> 

    <select class="form-control" id="id_category_mark_2" name="category_mark_2"> 
    <option value="" selected="selected">---------</option> 
     <option value="39">0 - 39 %</option> 
     <option value="49">40 - 49 %</option> 
     <option value="59">50 - 59 %</option> 
     <option value="69">60 - 69 %</option> 
     <option value="79">70 - 79 %</option> 
     <option value="80">80 or more</option> 
    </select> 

    <div id="late_submission_warning"></div> 

    <h4>General Comments</h4> 
    <textarea class="form-control" cols="40" id="id_comments" name="comments" rows="10"> 
    </textarea> 
    <br><br> 
    <input type = "submit" value="Save" class="btn btn-default"> 
    </form> 
</div> 

<script src="/static/js/jquery.min.js"></script> 
<script src="/static/js/bootstrap.js"></script> 
<script src="/static/js/bootstrap-datepicker.js"></script> 
<script src="/static/js/jquery.tablesorter.js"></script> 
<script src="/static/js/jquery.metadata.js"></script> 
<script src="/static/js/jquery.validate.min.js"></script> 
<script src="/static/js/bootbox.min.js"></script> 
<script src="/static/js/jquery.qtip.min.js"></script> 

<script type="text/javascript"> 

$(document).ready(function() 
    { 
    $("#sortable_table").tablesorter({ 
     sortList: [[0,0],[1,0]] 
     }); 
    } 
); 

</script> 

<script type="text/javascript"> 
$(document).ready(function(){ 

    $("#id_submission_date").datepicker({ 
     onSelect: function(dateText) { 
      console.log(dateText); 
     } 
    }); 

    $("#category_2").qtip({ 
     content: { 
      text: '80+ very full and perceptive awareness of issues, with original critical and analytical assessment of the issues and an excellent grasp of their wider significance.<br />70+ full and perceptive awareness of issues and a clear grasp of their wider significance.<br />60 - 69 adequate awareness of issues and a serious understanding of their wider significance.<br />50 - 59 some awareness of issues and their wider significance.<br />40 - 49 limited awareness of issues and their wider significance.<br />Below 40 very limited awareness of issues and of their wider significance.' 
      } 
     }); 
    }); 

</script> 

</body> 
</html> 
+0

请发布html以及 –

回答

0

那么它似乎有一个问题的地方与你的JavaScript和你将不得不看到控制台的信息看什么出错了。

无论如何,我希望在提出任何意见或完整解决方案之前查看完整的HTML和代码。但根据您的要求,为了简单起见,看到更改的日期已被正确捕获,我们必须遵循以下方法。

 $("#id_submission_date").datepicker({ 
     onSelect: function(dateText) { 
     console.log(dateText); 
     } 
    }); 

这里“dateText”应该包含所需的选定日期。检查您的浏览器控制台。但是如果出现错误,则会显示正确的错误。

一旦你得到合适的日期,你可以做比较。

错误的遗漏,请在这里发布错误消息,我们可以讨论更多。

+0

我已经使用了您指定的代码,但没有任何内容出现在控制台中 - 无论是在Chromium还是在Firebug中。 我可以发布完整的HTML代码,但它是一个Django页面,而且非常复杂。其他的一切都很好,所以我认为这个错误肯定在新代码中。 – Tobi

+0

如果console.log()没有在屏幕上打印任何内容,这意味着代码永远不会进入控制台。这可能意味着在执行datepicker代码之前出现错误,并且编译器永远不会到达这一点。 –

+0

奇怪的是,当我点击字段时,日期选择器本身就会显示出来。如果代码中有错误,通常不会那样做,是吗? – Tobi