gpt4 book ai didi

javascript - 在 django 中使用 Datepicker(来自 Jquery)

转载 作者:行者123 更新时间:2023-11-30 17:08:57 25 4
gpt4 key购买 nike

我想以这种方式在 django 上使用 Datepicker(来自 Jquery):

通过“views.py”,我从 mysql 数据库发送了一个日期列表。当用户选择的日期等于我在数据库中的日期之一时,我想显示一条消息。

我怎样才能做到这一点? (使用 Django、Jquery 和 AJAX)

到目前为止我的代码:

{% block head %}

<link href="{{STATIC_URL}}css/jquery-ui.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript" src="{{STATIC_URL}}js/jquery1.min.js"></script>
<script src="{{STATIC_URL}}js/jquery-ui.min.js"></script>


<script>
$(function() {
$( "#datepicker" ).datepicker({
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
});
date = $( "#datepicker" ).datepicker('getDate');
});
</script>

{% endblock %}

{% block body_block %}

<br/><br/><br/><br/><br/><br/><br/>
<form id="form" name="form" action="/home/" method="get">
<label>Choose Date</label>
<br/>
<input style="color:black" id="datepicker"/>
<br/><br/>
</form>

{% for date in dates %}
...............

谢谢。

最佳答案

这是一个工作示例: http://jsfiddle.net/njsmu356/

将所有日期添加到一个 div 并将其隐藏。

<div id="dateDiv" style="display:none;">
<ul id="dates">
{% for date in dates %}
<li>date</li>
{% endfor %}
</ul>
</div>

然后使用 jQuery 查找所选日期与 <li> 中的日期的匹配项.

    $('#datepicker').change(function() {
// Get the value of date field
var dateValue = $('#datepicker').val();

// Make and empty list
var dateList = [];

// Append all dates from <li> to dateList
$("#dates li").each(function() {
dateList.push($(this).text());
});

// Perform the match
if ($.inArray(dateValue, dateList ) > -1) {
alert("Date matched!!!");
} else {
alert("Sorry. Try again.");
}
});

注意:

请确保所选日期的格式与您从 View 中发送的日期的格式相同。您可能还需要验证日期的输入值。一些 jQuery 表单验证插件会有所帮助。

更新

既然你想用 AJAX 来做到这一点,那么已经有很多答案可以实现类似的功能。

  1. > Django: ajax response for valid/available username/email during registration
  2. > AJAX username validation in Django
  3. > And a lot here

关于javascript - 在 django 中使用 Datepicker(来自 Jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27384156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com