输入出生日期时,我需要显示年龄的帮助。我已经尝试过这种JS方法,但无法显示。
<div class="kidsFormCenter">
{% for kid in kids %}
<table class="kidsTable kidForm">
<form action="{% url 'update_profile_kid' kid.id %}" method="post">
{% csrf_token %}
<tr>
<td>First name:</td>
<td><input type="text" value="{{ kid.name }}" name="name"></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type="text" maxlength="10" value="{{ kid.dob }}" name="dob"></td>
<td><input type="text" id="age" class="form-control" required readonly></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="text" maxlength="1" value="{{ kid.gender }}" name="gender"></td>
</tr>
<tr>
<td><button class="btn btn-primary" formaction="{% url 'update_profile_kid' kid.id %}" type="submit">Edit</button>
<form action="{% url 'delete_profile_kid' kid.id %}" method="POST">
{% csrf_token %}<button class="btn btn-info" formaction="{% url 'delete_profile_kid' kid.id %}" type="submit">Delete</button></td>
</form>
</tr>
</form>
</table>
<br>
{% endfor %}
</div>
我的age.js是
$(function() {
$('input[name="dob"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true
},
function(start, end, label) {
var years = moment().diff(start, 'years');
$("#age").val("You are " + years + " years old.");
});
});