简体   繁体   English

jQuery .blur不起作用

[英]jQuery .blur doesn't work

I have 3 .blur's and only the first one works for some reason. 我有3个模糊对象,由于某种原因,只有第一个可用。 here's the jquery code: 这是jQuery代码:

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

and here's the form: 形式如下:

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

Before assuming blur() doesn't work you should test it with some simple code, eg: 在假定blur()不起作用之前,您应该使用一些简单的代码对其进行测试,例如:

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

I think the problem is you're looking for $("#passcheckbox"). 我认为问题是您正在寻找$(“#passcheckbox”)。 I don't see anything in your HTML with id="passcheckbox" . 我在您的HTML id="passcheckbox" It looks like you meant $( "#passwordcheckbox") . 看起来您的意思是$( "#passwordcheckbox")

Change #passcheckbox to #passwordcheckbox 将#passcheckbox更改为#passwordcheckbox

The rest of the code looks fine to me. 其余代码对我来说看起来不错。

通常,我只是将css添加到jquery选择器链中,以查看它们是否已正确选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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