所以我希望有一个显示cookie的响应式页面。我希望函数的值在输入值内的页面加载时显示,但我只能使用onclick函数显示它。这是我的代码:
<script type="text/javascript">
function readCookie(FullName) {
var name = FullName + "=";
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);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
</script>
<label>First Name from cookie: <input id="name2" maxlength="25" name="namefield" readonly="readonly" size="20" type="text" /> </label>
<input onclick="document.getElementById('name2').value = readCookie('FullName')" type="submit" value="Get cookie" />
如何在不使用提交按钮的情况下让函数在输入中显示结果?
答案 0 :(得分:0)
在<script>
之后使用提交按钮<input>
属性的内容制作一个onclick
元素:
<script type="text/javascript">
function readCookie(FullName) {
var name = FullName + "=";
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);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
</script>
<label>First Name from cookie: <input id="name2" maxlength="25" name="namefield" readonly="readonly" size="20" type="text" /> </label>
<script>
document.getElementById('name2').value = readCookie('FullName')
</script>