![](/img/trans.png)
[英]how to create a form which will only allow to submit if the input is not empty and is > 0?
[英]How to pass form user input through to the function which runs on form submit and create a cookie with the name of the users input?
下面的代码成功创建了带有字符串值“ XinputX”的cookie。 但是,我希望将其作为用户输入的价值。
我还需要单击相同的另一页,如果已经使用onSubmit设置cookie了,该将动作放在哪里?
function createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else var expires = ""; document.cookie = name + "=" + value + expires + "; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } function eraseCookie(name) { createCookie(name, "", -1); }
<div class="container-fluid"> <div class="row"> <div class="d-flex mx-auto mt-5 mb-5"> Hello and welcome to the quiz. </div> </div> <div class="row"> <div class="d-flex mx-auto mb-5"> <form onsubmit="createCookie('first_cookie','XinputX',7)"> Enter name: <input type="text"> <input type="submit"> </form> </div> </div> <div class="row"> <div class="col-12 col-sm-6 mx-auto"> <button type="button" id="nextQuestion" class="btn btn-primary d-flex mx-auto mt-2">Lets go!</button> </div> </div> </div>
我用console.log替换了cookie代码,因为SO不允许设置cookie。 上传时只需删除//
我还命名了该字段并为其指定了ID
function createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else var expires = ""; // document.cookie = name + "=" + value + expires + "; path=/"; console.log("cookie",name + "=" + value + expires + "; path=/") } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } function eraseCookie(name) { createCookie(name, "", -1); } window.addEventListener("load", function() { document.getElementById("myForm").addEventListener("submit", function(e) { e.preventDefault(); // stop submission, remove this if you add an action to the form createCookie('first_cookie', this.textField.value,7); // if you do not want to use the action attribute of the form you can do this: // window.location="somewhereelse.html"; }); });
<div class="container-fluid"> <div class="row"> <div class="d-flex mx-auto mt-5 mb-5"> Hello and welcome to the quiz. </div> </div> <div class="row"> <div class="d-flex mx-auto mb-5"> <form id="myForm"> Enter name: <input name="textField" type="text"> <input type="submit"> </form> </div> </div> <div class="row"> <div class="col-12 col-sm-6 mx-auto"> <button type="button" id="nextQuestion" class="btn btn-primary d-flex mx-auto mt-2">Lets go!</button> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.