admin管理员组文章数量:1419244
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
4 Answers
Reset to default 5This 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
本文标签: javascriptsetTimeout on form submission returning 39not a function39 errorStack Overflow
版权声明:本文标题:javascript - setTimeout on form submission returning 'not a function' error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300857a2652362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论