最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .reset() function causing error instead of executing - Stack Overflow

programmeradmin2浏览0评论

For some reason I'm having trouble coding in to the reset button. I have this code:

 <form name="sub">
   ...
   <button type=submit>Submit</button>
   <button type=reset>Reset</button>
 </form>

When I click reset, it reset's the form, as usual. However, when I try to bind a manual reset option on the button, like this code, it returns an error:

 var $f = $("form[name=sub]");

 $f.find("button[type=reset]").click(function(e){
   e.preventDefault();
   $f.reset(); // <-- Uncaught TypeError: undefined is not a function
   $(window).scrollTop(0);
 });

I get this error: Uncaught TypeError: undefined is not a function

Tried using document.form.sub.reset(); and tried $f = (global variable) instead of var $f = that doesn't work either. What's going on ?

For some reason I'm having trouble coding in to the reset button. I have this code:

 <form name="sub">
   ...
   <button type=submit>Submit</button>
   <button type=reset>Reset</button>
 </form>

When I click reset, it reset's the form, as usual. However, when I try to bind a manual reset option on the button, like this code, it returns an error:

 var $f = $("form[name=sub]");

 $f.find("button[type=reset]").click(function(e){
   e.preventDefault();
   $f.reset(); // <-- Uncaught TypeError: undefined is not a function
   $(window).scrollTop(0);
 });

I get this error: Uncaught TypeError: undefined is not a function

Tried using document.form.sub.reset(); and tried $f = (global variable) instead of var $f = that doesn't work either. What's going on ?

Share Improve this question asked Jul 28, 2014 at 10:18 Control FreakControl Freak 13.2k31 gold badges99 silver badges150 bronze badges 1
  • 4 You should be doing $f[0].reset() – MayThrow Commented Jul 28, 2014 at 10:21
Add a ment  | 

2 Answers 2

Reset to default 7

reset() is a DOM method so use it on DOM object $f[0]

Then you try DOM 0 via document.form.sub.reset(); you have misprint:

document.forms.sub.reset();

You can use it in jQuery, just get access to node:

 $("form[name=sub]").find("button[type=reset]").click(function(e){
   e.preventDefault();
   $f.get(0).reset();//jQuery.fn.get return node, not jQuery object
   $(window).scrollTop(0);
 });
发布评论

评论列表(0)

  1. 暂无评论