从 html 表单设置 cookie
Set cookies from html form
我尝试通过 onsubmit 事件从我的 html 表单设置 cookie。但是控制台 return "Uncaught ReferenceError: setCookies is not defined"
这是我的一段代码:
<form action="page.html" method="POST" onsubmit=" return setCookies(this)" id="myFormId">
<input name='user' placeholder='user'><br>
<input name='password' type='password' placeholder='password'><br>
<button type='submit'>Try</button>
</form>
<h1>HWETEYT</h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
var setCookies = (function(form){
console.log(form[0].value)
for (i = 0; i < form.length; i++){
document.cookie = form[i] + "=" + form[i].value;
}
console.log(document.cookie);
}
});
</script>
我尝试将我的函数设为全局函数,但结果相同。
由于你在script标签中添加了src属性,所以里面写的代码被忽略了。因此,您必须将脚本拆分为两个脚本;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
var setCookies = function(form){
console.log(form[0].value)
for (i = 0; i < form.length; i++){
document.cookie = form[i] + "=" + form[i].value;
}
console.log(document.cookie);
}
</script>
我尝试通过 onsubmit 事件从我的 html 表单设置 cookie。但是控制台 return "Uncaught ReferenceError: setCookies is not defined"
这是我的一段代码:
<form action="page.html" method="POST" onsubmit=" return setCookies(this)" id="myFormId">
<input name='user' placeholder='user'><br>
<input name='password' type='password' placeholder='password'><br>
<button type='submit'>Try</button>
</form>
<h1>HWETEYT</h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
var setCookies = (function(form){
console.log(form[0].value)
for (i = 0; i < form.length; i++){
document.cookie = form[i] + "=" + form[i].value;
}
console.log(document.cookie);
}
});
</script>
我尝试将我的函数设为全局函数,但结果相同。
由于你在script标签中添加了src属性,所以里面写的代码被忽略了。因此,您必须将脚本拆分为两个脚本;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
var setCookies = function(form){
console.log(form[0].value)
for (i = 0; i < form.length; i++){
document.cookie = form[i] + "=" + form[i].value;
}
console.log(document.cookie);
}
</script>