简体   繁体   中英

How do I resolve JavaScript error “not defined”?

This form works in Safari. In IE, it mostly works, but I get an error "Object doesn't support this property or method." on line 240.

In Firefox, no alert box appears, but the following error appears in the error console:

Error: myform is not defined Line: 240 (line 240 is below, starting with the word if)

<script type="text/javascript">
// Validate the form
// Confirm with the visitor the amount he entered is correct
// Submit if OK is pressed
$(document).ready(function() {
    $("#myform").validate({
        submitHandler:function(form){
            if(confirm("Please confirm your donation of $"+ myform.x_Amount.value + " to us.")){
                form.submit();
            }
        }
    });
});

// Put grey "no spaces" example text in the credit card number field
$('#x_Card_Num').example('(NoSpaces)', {
  className: 'greydefaulttext'
});
</script>

Guess I need to define myform.x_Amount.value. Have tried doing this in various ways but haven't stumbled across the right location to define it yet, or even the right syntax. Thanks for any help.

Update: Thanks to fresh eyes from RC and Bobince, it was simply a typo: myform.x_Amount.value should be named form.x_Amount.value - it now works in all browsers.

"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.

When you name the button submit, you override the submit() function on the form.

source

that fixes your one error. If by form.x_Amount.value you want to get the value of a textfield, try it like this:

document.form_name.element_name.value;

tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM