2011-03-08 103 views
0

我得到這樣的錯誤:快速jQuery的問題

Uncaught TypeError: Cannot read property 'form' of undefined

螢火蟲說,此行是罪魁禍首:

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,如果它是有效的,但我只是得到我上面提到的錯誤而不是y結果。

編輯:下面是形式的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> 
+0

什麼是您的HTML看起來像的形式? – justkt 2011-03-08 19:16:13

+1

這只是一個錯字,你忘了哈希...應該是'$(「#emailPost2」)' – mVChr 2011-03-08 19:17:34

回答

7

貌似這個問題很簡單,你忘了ID選擇的#:

$("#emailPost2").valid() 

(而不是當前$("emailPost2").valid()

2

假設
form id =「emailPost2」

你需要

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

我想大膽的,但不允許在括號內,因此我爲15秒慢:(

+0

hehe。早起的鳥抓住了蠕蟲^ _ ^ – Neal 2011-03-08 19:21:03