问题描述
我正在尝试使用jQuery重置某些表单,但遇到了一些麻烦.除了涉及编写函数或自定义插件的解决方案之外,我一直在不断寻找复位方法不是jQuery的标准部分,而是标准Javascript的一部分.
I'm trying to reset some forms with jQuery but I'm having some trouble. Aside from solutions that involve writing functions or custom plugins, I keep finding time and again that the reset method is not a standard part of jQuery but it is a part of standard Javascript.
无论如何,我的第一个方法是和
Anyway, my first approach was to go with
$("#theForm").reset();
其中表单的id ="theForm".
where the form's id="theForm".
那显然不起作用,因为它假定reset()是jQuery的一部分.
That didn't work obviously, because it assumed that reset() was a part of jQuery.
我接下来尝试的是
document.getElementById(theForm).reset();
哪个也不起作用.我是jQuery的新手,所以不确定是否可以将普通Javascript与jQuery混合使用.我一定听起来像个白痴.
Which didn't work either. I'm new to jQuery so I'm not sure if I can mix normal Javascript with jQuery. I must sound like a moron for saying this.
无论如何,在进行了更多研究之后,我一次又一次发现我可以通过执行以下操作在jQuery中使用reset()...
Anyway, After doing some more researching I found time and again that I could use reset() in jQuery by doing these...
$("#theForm")[0].reset();
或
$("#theForm").get(0).reset();
在涉及这两个摘要的每篇文章中,评论中的每个人都使它起作用.
In every article that involves these two snippets everyone in the comments had gotten it working.
除了我.
错误控制台不断告诉我,我写的内容不存在.我已经检查了所有代码,并且没有其他单词"reset"的实例,所以也不能如此.
The error console keeps telling me that what I have written does not exist. I have checked all of my code and there is no other instance of the word "reset", so it can't be that either.
反正我很困惑.
推荐答案
此线程中原始问题的解决方案
对于这样的HTML表单:
Solution to original issue in this thread
For an HTML form like this:
<form id="theForm">
</form>
使用document.getElementById("theForm").reset()
时,如果出现js错误提示
When using document.getElementById("theForm").reset()
, if you are getting a js error saying
原因可能是具有reset
作为name
或id
的元素.我遇到了同样的问题.
将任何元素命名为reset
会覆盖reset()函数.
the reason might be an element which has reset
as name
or id
. I faced same issue.
Naming any element as reset
overrides reset() function.
这篇关于$("...")[0] .reset不是函数...正在jQuery中重置表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!