没有使用表单就会记住输入 [英] input remembered without using a form

查看:96
本文介绍了没有使用表单就会记住输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用表单并提交按钮,那么浏览器可以记住文本输入字段,但是如果我仅使用输入类型,没有表单并且按钮操作为onclick事件,那么浏览器不会缓存输入值用户。



有没有办法让浏览器在不使用实际提交的情况下缓存表单输入?

<下面是示例代码,显示我的页面。当用户点击搜索时,会调用一个javascript函数来填充div结果。

 < h2>设备清理程序< / h2> 
< p>输入要清洁的设备的MAC地址:< / p>
< input type =textid =entryname =macaddronkeypress =keyChecker(event)>
< button type =buttonid =startonclick =query()>搜寻< /按钮>
< div id =results>< / div>
< / body>
< / html>


解决方案

不可能强制浏览器保存这个信息。只要您想要保存的输入相对较短,您就可以使用JavaScript来存储包含此信息的Cookie。然后,在将来查看该页面时,可以使用这些cookie填充输入字段。请注意敏感信息。



或者,您可以通过AJAX请求将用户输入存储在您自己的服务器上。


If I use a form and submit button then the browser can remember the text input fields, but if I use only input types, no form and have the button action an onclick event, then the browser does not cache input values for the user.

Is there a way to have the browser still cache "form" inputs without using an actual submit?

below is sample code, showing my page. When the user clicks search, then a javascript function is called that will populate the div "results". Everything works except no browser will cache the text input.

<h2>Device Cleaner</h2>
<p>Enter MAC Address of device to clean:</p>
<input type="text" id="entry" name="macaddr" onkeypress="keyChecker(event)">
<button type="button" id="start" onclick="query()">Search</button>
<div id="results"></div>
</body>
</html>

解决方案

It is not possible to force a browser to save this information. As long as the input you would like saved is relatively short, you could use JavaScript to store cookies with this information. Then when viewing the page in the future, you can use those cookies to fill the input fields. Be wary of sensitive information though.

Alternatively, you could store the user input on your own server through AJAX requests.

这篇关于没有使用表单就会记住输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆