简体   繁体   English

JavaScript 用今天的日期填充两个表单字段

[英]JavaScript populate two form fields with Today's Date

I have the code in JSfiddle .我在JSfiddle中有代码。 But two things don't happen.但是有两件事不会发生。 Unchecking the box only affects one field ( entry_date ) and when I submit the form the field expiration_date does not get written to the DB -- the entry_date does.取消选中该框只会影响一个字段( entry_date ),当我提交表单时,字段expiration_date不会被写入数据库 - entry_date会。 I have jerry rigged the script.我已经杰瑞操纵了剧本。 I know nothing about JS so any tips would be welcome.我对JS一无所知,所以欢迎任何提示。

 function onCheck(checkbox) { var entry_date = document.getElementById('entry_date'); var expiration_date = document.getElementById('expiration_date'); entry_date.disabled = checkbox.checked; expiration_date.disabled = checkbox.checked; if (checkbox.checked) { //entry_date.value = new Date().toISOString().substr(0, 10); //expiration_date.value = new Date().toISOString().substr(0, 10); entry_date.valueAsDate = new Date(); expiration_date.valueAsDate = new Date(); } else entry_date.value = ''; }
 Day Guest<br> <input type="checkbox" id="myCheck" name="dayguest" onclick="onCheck(this);"> <!--If checkbox is checked date box is disabled--> <br> <!--Gets today's date and puts it as max--> <label for="entry_date">Arrival Date</label> <input type="date" id="entry_date" name="entry_date"> <br> <label for="entry_date">Departure Date</label> <input type="date" id="expiration_date" name="expiration_date">

Your form code isn't included, so I can't speak to the backend/database issue, but the reason unchecking the box only affects one field is because your else statement only changes one value, not both:您的表单代码不包括在内,因此我无法谈论后端/数据库问题,但取消选中该框仅影响一个字段的原因是因为您的else语句仅更改一个值,而不是两个:

entry_date.value = '';

Adding a corresponding statement to reset the second field will address that problem:添加相应的语句来重置第二个字段将解决该问题:

expiration_date.value = '';

Not related to the problem at hand, but your second <label> is corresponding to entry_date rather than expiration_date (fixed in the below snippet).与手头的问题无关,但您的第二个<label>对应于entry_date而不是expiration_date (在下面的代码段中修复)。

 function onCheck(checkbox) { var entry_date = document.getElementById('entry_date'); var expiration_date = document.getElementById('expiration_date'); entry_date.disabled = checkbox.checked; expiration_date.disabled = checkbox.checked; if (checkbox.checked) { //entry_date.value = new Date().toISOString().substr(0, 10); //expiration_date.value = new Date().toISOString().substr(0, 10); entry_date.valueAsDate = new Date(); expiration_date.valueAsDate = new Date(); } else { entry_date.value = ''; expiration_date.value = ''; } }
 Day Guest<br> <input type="checkbox" id="myCheck" name="dayguest" onclick="onCheck(this);"> <!--If checkbox is checked date box is disabled--> <br> <!--Gets today's date and puts it as max--> <label for="entry_date">Arrival Date</label> <input type="date" id="entry_date" name="entry_date"> <br> <label for="expiration_date">Departure Date</label> <input type="date" id="expiration_date" name="expiration_date">

The first problem is simple: you forgot to assign to expiration_date.value in the else block.第一个问题很简单:您忘记在else块中分配给expiration_date.value

The second problem is that disabled inputs are not submitted in forms.第二个问题是 forms 中没有提交禁用的输入。 If you want to prevent the user from changing the value, set the readonly attribute.如果要防止用户更改值,请设置readonly属性。

 function onCheck(checkbox) { var entry_date = document.getElementById('entry_date'); var expiration_date = document.getElementById('expiration_date'); if (checkbox.checked) { entry_date.valueAsDate = new Date(); expiration_date.valueAsDate = new Date(); entry_date.setAttribute("readonly", true); expiration_date.setAttribute("readonly", true); } else { entry_date.value = ''; expiration_date.value = ''; entry_date.removeAttribute("readonly"); expiration_date.removeAttribute("readonly"); } }
 Day Guest<br> <input type="checkbox" id="myCheck" name="dayguest" onclick="onCheck(this);"> <!--If checkbox is checked date box is disabled--> <br> <!--Gets today's date and puts it as max--> <label for="entry_date">Arrival Date</label> <input type="date" id="entry_date" name="entry_date"> <br> <label for="expiration_date">Departure Date</label> <input type="date" id="expiration_date" name="expiration_date">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM