简体   繁体   中英

Quick jQuery Question

I get an error like this:

Uncaught TypeError: Cannot read property 'form' of undefined

Firebug says this line is the culprit:

if($("emailPost2").valid())

Here's all my jQuery code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>

<script type="text/javascript" src="https://github.com/jzaefferer/jquery-validation/raw/master/jquery.validate.js"></script>

<script type="text/javascript" charset="utf-8">
  $(function(){      
     $("emailPost2").validate({
        rules: {
            emailAddress: {
                required: true,
                email: true
                      }
            }
     }); 
     $('#zonePlus').click(function() {
          $('#zoneNotif').submit();
     });

     $('#searchPost').submit(function(event) {
     if ($(this).find('#searchBox').val() == '') {
          event.preventDefault();
     }
     });
     $("#searchBox").autocomplete({
             source: 'php/searchAC.php'
     });
     $("button, input:submit, input:button, a#jql, input:radio").button();

     $('#emailJQButton').live('click',function() {
        $("#emailModal").dialog('open');
     });

     $('#eb1').live('click',function() {
        $('#emailPost').submit();
        $("#emailModal").dialog('close');
     });

     $('#eb2').live('click',function() {
        if($("emailPost2").valid())
        {
            $('#emailPost2').submit();
            $("#emailModal").dialog('close');
        }
     });
});
</script>

valid() should return true if it is valid, but I just get the error I mentioned above instead of any results.

<div id="emailModal">
<form action="php/emailPost.php" method="POST" class="inline" id="emailPost2">
<label name="error"></label>
<input type="text" value="Enter an Email" class="required email" name="emailAddress"     style="display: inline-block;">
<input type="button" value="Email" id="eb2"/>
<input type="hidden" name="passedCoupID" value="1"/>
</form>
</div>

Looks like the problem is simply that you forgot the # in the id selector:

$("#emailPost2").valid()

(rather than the current $("emailPost2").valid() )

Assuming
form id="emailPost2"

you need

if($("#emailPost2").valid())

I wanted to BOLD the # but was not allowed inside the brackets, hence I was 15 seconds slower :(

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