admin管理员组

文章数量:1335371

Code

html

<form id="myFrm" name="myFrm" action="/myApp/ProcessFrm" method="post">
    <input autofocus="autofocus" type="text" id="Location" name="Location" onkeydown="changetotab()" style="width: 4em" tabindex="1"  />
    <input type="Submit" value="Submit" id="submit" /> <br />
</form>

js

 function changetotab() {
            var charCode = (window.event.which) ? window.event.which : window.event.keyCode;

            if (charCode == 13 || charCode == 9) {
                if (document.myFrm.submit == undefined) {
                    alert("form submit can't be found")
                } else {
                    alert("form can be submitted");
                   document.myFrm.submit();
                }
            }
        }

What happens is that when the user pressed TAB or ENTER, document.myFrm.submit == undefined es up false (so far so good); then something weird happens - when I get to document.myFrm.submit(); JS informs that this method is not found.

0x80020003 - JavaScript runtime error: Member not found.

What is happening here?

Note: I can't use Jquery, just in case you were wondering why I am using old style JS. This will run on the old devices that do not support Jquery.

Code

html

<form id="myFrm" name="myFrm" action="/myApp/ProcessFrm" method="post">
    <input autofocus="autofocus" type="text" id="Location" name="Location" onkeydown="changetotab()" style="width: 4em" tabindex="1"  />
    <input type="Submit" value="Submit" id="submit" /> <br />
</form>

js

 function changetotab() {
            var charCode = (window.event.which) ? window.event.which : window.event.keyCode;

            if (charCode == 13 || charCode == 9) {
                if (document.myFrm.submit == undefined) {
                    alert("form submit can't be found")
                } else {
                    alert("form can be submitted");
                   document.myFrm.submit();
                }
            }
        }

What happens is that when the user pressed TAB or ENTER, document.myFrm.submit == undefined es up false (so far so good); then something weird happens - when I get to document.myFrm.submit(); JS informs that this method is not found.

0x80020003 - JavaScript runtime error: Member not found.

What is happening here?

Note: I can't use Jquery, just in case you were wondering why I am using old style JS. This will run on the old devices that do not support Jquery.

Share Improve this question asked Apr 4, 2014 at 16:48 sarsnakesarsnake 27.7k62 gold badges185 silver badges294 bronze badges 13
  • did you try document.getElementById('myFrm') instead of document.myFrm? – Will N Commented Apr 4, 2014 at 16:52
  • or document.forms["myFrm"] – Will N Commented Apr 4, 2014 at 16:53
  • Just did try both - neither works. Trying not to rip my hair out. Let me reboot my Visual Studio. – sarsnake Commented Apr 4, 2014 at 16:55
  • nope, no luck..... any ideas would be wele at this point – sarsnake Commented Apr 4, 2014 at 16:56
  • 1 Before submitting do alert(typeof document.myFrm.submit). What you see? – Teemu Commented Apr 4, 2014 at 16:57
 |  Show 8 more ments

2 Answers 2

Reset to default 10

See this JSfiddle. What's going on here? Well, in the pre-DOM times, JavaScript created one property foo for each input with name foo on the input's parent form:

form.foo.toString() === "HTMLInputElement";

This however, means, that, if you have an input element with name submit, the form's own submit method is overwritten. This behaviour has survived till today.

Now, your input has no name attribute. In this case, the behaviour is to deduce one from the input's ID, if one is set. So basically, change the ID of the input, that's the most straight-forward way to deal with it:

<input type="Submit" value="Submit" id="submit_" />

There are lots of such subtleties, unfortunately. See DOMLint for a good overview.

Edit: What to do, if you end up with a clobbered submit method and no way to change the HTML? Then fetch the submit method from the form's prototype and use that:

// form.submit() doesn't work, but this will:
HTMLFormElement.prototype.submit.call(form);

Try this (plain javascript):

document.getElementById('myFrm')

本文标签: Formsubmit is not found in JavascriptStack Overflow