快速的jQuery问题

时间:2011-03-08 19:14:36

标签: javascript jquery forms

我收到这样的错误:

Uncaught TypeError: Cannot read property 'form' of undefined

Firebug说这条线是罪魁祸首:

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

这是我所有的jQuery代码:

<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()应该返回true,但是我只是得到了上面提到的错误,而不是任何结果。

编辑:以下是表单的HTML代码:

<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>

2 个答案:

答案 0 :(得分:7)

看起来问题只是忘记了id选择器中的#:

$("#emailPost2").valid()

(而不是当前的$("emailPost2").valid()

答案 1 :(得分:2)

假设
表格id =“emailPost2”

你需要

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

我想要烧,但不允许在括号内,因此我的速度慢了15秒:(