jQuery函数清除表单并设置回onLoad状态
对于我的 jq 驱动表单,我正在寻找一个函数来清除我的表单并将其设置回页面加载时的原始阶段,但不刷新整个页面。我使用占位符文本并创建了一个标准重置按钮。在 webkit 浏览器中它工作正常。如果按下重置按钮,表单将返回到原始状态,显示占位符文本,但在 IE 中(当前使用 8),表单会被清除,但占位符文本不会返回。知道如何清除表单但取回我的占位符文本吗(我正在使用 placeholder.js 插件)?我尝试了以下方法,但不起作用:
$("#refresh").click(function() {
$('#name, #email, #betreff, #nachricht').val('');
});
有什么帮助吗?
编辑
链接到占位符插件:http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
...是的,我现在正在谈论在帮助下创建它jquery 因为它不能用标准的 html 重置按钮来完成。我的意思是可以,但在 IE 中不行。 IE 清除表单并且不再显示占位符文本。 Webkit 浏览器与标准重置按钮配合良好,再次显示占位符文本。现在我正在寻找一种解决方案,以在所有浏览器中以相同的方式使用它。
For my jq driven form I am looking for a function that clears my form and set it back to the original stage as it was on page load but without refreshing the entire page. I use placeholder text and I have created a standard reset button. In webkit browsers it works fine. If the reset button is hit, the form returns to the original state, showing the placeholder text BUT in IE (currently using 8) the form get cleared but the placeholder text is not comming back. Any idea how I can clear the form but getting back my placeholder text (I am using the placeholder.js plugin for that)? I tried the following but it doesn´t work:
$("#refresh").click(function() {
$('#name, #email, #betreff, #nachricht').val('');
});
Any help out there?
EDIT
Link to the placeholder plugin:http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
...and yes, I am talking about foing it now with the help of jquery because it can´t be done with a standard html reset button. I mean it can but not in IE. IE clears the form and does not show the placeholder text again. Webkit browser are working fine with that standard reset button, showing the placeholder text again. Now I am looking for a solution to have it the same way in all browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 id="refresh" 设置为标准按钮,以免清除整个表单。您必须通过以下函数手动“重置”使用占位符的表单元素:
您必须使用 val(''); 清除非占位符元素;
您还可以尝试一种更通用的方法,即使用 placholder 属性选择所有相关的表单元素(假设您在 HTML 中或在此脚本运行之前执行了此操作):
这应该替换所有 input/textarea 元素,以便您将替换所有其他元素必须添加到选择器。
Make the id="refresh" just a standard button as to not clear the entire form. You will have to manually "reset" the form elements that use the placeholder via the function below:
You will have to clear the non-placeholder elements using val('');
You can also try a more general approach which is to select all related form elements with the placholder attribute (assuming you did so in your HTML or before this script runs):
This should replace all the input/textarea elements so any other elements you will have to add to the selector.
这有效吗? (我无法从这里测试)
如果它有效,您可能需要使用
input
和一些选择器的快捷方式。Is this working ? (I can't test from here)
If it works, you may want to use shortcuts with
input
and some selectors.今天正在研究类似的问题。我希望这有帮助。
这要归功于保罗·贝甘蒂诺。
Was working on a similar problem today. I hope this helps.
Credit goes to Paolo Bergantino.