2009-07-22 37 views
1

我有3個.blur,只有第一個出於某種原因工作。這裏的jQuery代碼:jQuery .blur不起作用

<script type='text/javascript'> 
    $(document).ready(function() { 
     $("#user_name").blur(function() { 
      if ($(this).val().length > 4) { 
       $("#usernamecheckbox").html("<img src='images/checkmark.png' alt='' />"); 
      } else { 
       $("#usernamecheckbox").html("<img src='images/xmark.png' alt='' />"); 
      } 
     }); 
     $("#pass").blur(function() { 
      if ($(this).val().length < 4) { 
       $("#passcheckbox").html("<img src='images/xmark.png' alt='' />"); 
      } 
     }); 
     $("#confirmpass").blur(function() { 
      if ($(this).val() != $("#pass").val()){ 
       $("#passcheckbox").html("<img src='images/xmark.png' alt='' />"); 
      } else { 
       $("#passcheckbox").html("<img src='images/checkmark.png' alt='' />"); 
      } 
     }); 
    }); 
</script> 

和這裏的形式:

<form enctype="multipart/form-data" method='post' action='reg.php'> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
    <p>Username <span class='sub'>must be atleast 4 characters</span></p> 
    <input type='text' name='user_name' id='user_name' /> <div id='usernamecheckbox' class='checkbox'></div><br /> 
    <p>Password <span class='sub'>must be atleast 4 characters</span></p> 
    <input type='password' id='pass' name='pass' /> &nbsp; <input type='password' name='confirm' id='confirmpass' /> <div id='passwordcheckbox' class='checkbox'></div><br /> 
    <p>E-mail <span class='sub'>must be a valid E-mail address</span></p> 
    <input type='text' name='email' id='email' size='40' /> <div id='emailcheckbox' class='checkbox'></div><br /> 
    <p>Avatar</p> 
    <input type='file' name='avatar' /><br /><br /> 
    <?php 
    require_once('recaptchalib.php'); 
    $publickey = "6LdLYwcAAAAAAEeupJKr_IBviIq-xHX5W98IacgZ"; // you got this from the signup page 
    echo recaptcha_get_html($publickey); 
    ?> 
    <br /> 
    <input type='submit' value='Submit'> 
</form> 
+0

要檢查長度> 4,長度<4的if語句您是否獲得了checkmark.png或第一個xmark.png,它工作 – amischiefr 2009-07-22 20:02:42

+0

井?第一個如果內容有超過4個字符我得到複選標記,如果它沒有,那麼我得到xmark。 – 2009-07-22 20:03:50

回答

11

假設模糊()不工作,你應該用一些簡單的代碼進行測試之前,如:

$("#pass").blur(function() { 
      alert('#pass blur triggered'); 
    }); 

我認爲問題在於您正在尋找$(「#passcheckbox」)。我在id="passcheckbox"的HTML中沒有看到任何內容。它看起來像你的意思是$("#passwordcheckbox")

+5

我總是喜歡也提供基本故障排除建議的答案。 +1 – Travis 2009-07-22 20:30:01

2

變化#passcheckbox到#passwordcheckbox

的代碼的其餘部分看起來好像沒什麼問題。

0

我經常只添加CSS到jQuery選擇鏈,看看是否他們已經正確選擇:

$('#passcheckbox').css({'background-color':'red'}).etc();