javascript - setTimeout on form submission returning 'not a function' error - Stack Overflow

I have the following: var thiscode = {init: function(){jQuery('#submit').bind('click�

I have the following:

var thiscode = {
init: function(){
    jQuery('#submit').bind('click',thiscode.clickListener);
},

clickListener: function(event){
    setTimeout('document.myform.submit()',5000);
    return false;
}
};
thiscode.init();

setTimeout is working, but when it fires, I get this: "document.myform.submit is not a function".

Any idea why, and/or how to fix it?

I have the following:

var thiscode = {
init: function(){
    jQuery('#submit').bind('click',thiscode.clickListener);
},

clickListener: function(event){
    setTimeout('document.myform.submit()',5000);
    return false;
}
};
thiscode.init();

setTimeout is working, but when it fires, I get this: "document.myform.submit is not a function".

Any idea why, and/or how to fix it?

Share Improve this question asked Oct 12, 2010 at 16:30 mjsiemermjsiemer 891 silver badge9 bronze badges 1
  • 2 Are you sure the name of your form is "myform"? – Josh Stodola Commented Oct 12, 2010 at 16:34
Add a ment  | 

4 Answers 4

Reset to default 5

This likely has nothing to do with your use of setTimeout (which has issues raised by other people).

The most likely cause is that you have an element named submit (or with that as an id) inside the form (probably the one that you are matching with your jQuery selector) and that this has replaced the function that was on the submit property with an HTMLElementNode.

The easiest solution is to rename the element.

Don't pass a string to setTimeout, pass an anonymous function...

clickListener: function(event) {
  setTimeout(function() {
    document.myform.submit();
  }, 5000);
  return false;
}

EDIT: Just had a revelation. If you have an element in your form with a name/id of "submit", that will override the submit function in the DOM (document.myform.submit will now refer to that element instead of the function). If this is the case, you'll need to rename that element.

setTimeout can also take a function handle - try passing document.myform.submit without the quotes:

setTimeout(document.myform.submit,5000);

David's answer is good, but you really should not be binding click, what if the user presses enter to submit the form? They've just gone around your procedure.

Instead bind .submit(). You can use a variable to control if there is a submit:

var submitNow = false;
$(function() {  
      // Set up the submit handler
    $("form").submit(function() {

          // In 5 seconds...
        setTimeout(function() {

              // Set the submit variable to true
            submitNow = true;

              // Trigger the submit
            $("form").submit();
        }, 5000);

          // This will only submit if submit variable is true.
        return submitNow;
    });
});​

Try it out with this jsFiddle example

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300858a4621426.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信