javascript - jQuery .click() doesn't trigger function in IE -
i have html form runs javascript function before submitting. html of form tag is:
<form onsubmit="return sub();" method="post" action="useclick.php">
the html of submit button is:
<input type="submit" value="submit" id="submit"/>
that works fine, want user able press enter anywhere , submit form i've got bit of code:
$(document).keypress(function(e) { if(e.which == 13) { $(this).blur(); $('#submit').focus().click(); } });
this want in browser except ie. in ie, click submit button, , send off form doesn't run sub() function first. it's same in ie 8 , 10, versions have access @ moment.
for completeness, sub() function looks like:
function sub(){ document.getelementsbyname('coords')[0].value = json.stringify(coordsarray); return true; }
i'm new javascript , new jquery useful.
have tried:
$(document).keypress(function(e) { if(e.which == 13) { $(this).blur(); $('#your_form_id').submit(); return false; } });
in sense programmatically submitting , preventing subsequent chain of events.
update: changed , made a jsfiddle demo. let me know if works you.
<h2>form</h2> <form action="" method="post" id="myform"> first name:<input type="text" name="fname" maxlength="12" size="12"/> <br/> last name:<input type="text" name="lname" maxlength="36" size="12"/> <br/> <p><input id="submit" type="submit" /></p> </form> <h2>json</h2> <pre id="result"> </pre> <script> $.fn.serializeobject = function() { var o = {}; var = this.serializearray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; $(' input').keypress(function(e) { if(e.which == 13) { console.log('enter pressed'); $('#submit').blur(); $('#myform').submit(); e.preventdefault(); return false; } }); $(function() { $('form').submit(function() { console.log('submitted'); $('#result').text(json.stringify($('form').serializeobject())); return false; }); }); </script>
update #2 hi, need make sure bind elements want listen to. in case elements in document. in general listen key events elements should focusable. in new jsfiddle enter press event attached mentioned. make sure focus (read: click) anywhere in document first. let me know how goes.
update #3 added jsfiddle make sure body focused , has tabindex. please accept answer if works you.
Comments
Post a Comment