jQuery .submit没有被触发?

时间:2011-09-07 10:12:19

标签: jquery css

我正在使用此代码:

$j("#register-form form").submit(function() {
  if ($j("input:first").val() == "correct") {
    $j("#registration-notification").animate({
            height: "21px",
            opacity: 1,
            'padding-top': "8px"
        }, 800 );
    return true;
  }
  $j("span").text("Not valid!").show().fadeOut(1000);
  return false;
});

在此表格上(来自萤火虫的代码):

<div id="register-form">
<form class="bbp-login-form" action="http://localhost/taiwantalkorgfinal2/wp-login.php" method="post">
<fieldset class="bbp-form">
<h5>Create a Taiwantalk account</h5>
<div class="bbp-username">
<div class="bbp-email">
<div class="bbp-submit-wrapper">
</fieldset>
</form>

显示此通知:

HTML:

<div id="registration-notification">
    <div id="message">
        <span><?php _e( 'Check your email for the username and password' ); ?></span>
        <a href="#" id="close-button">x</a>
    </div>
</div><!-- #container -->

CSS:

#registration-notification {
    background-color: #444;
    color: #FFF;
    height: 0;
    opacity: 0;
    padding: 0;
    overflow: hidden;
    #close-button {
        background: #888;
        color: #FFF;
        float: right;
        padding: 0 3px;
        border-radius: 2px;
        -moz-border-radius: 2px;
        -webkit-border-radius: 2px;
        behavior: url(PIE.htc);
        &:hover {
            background: #BA422B;
            color: #FFF;
        }
    }
    #message {
        margin: 0 auto;
        width: 940px;
        span {
            float: left;
            width: 720px;
        }
        a {
            color: #FFF;
            font-weight: bold;
        }
    }
}
#header {
    overflow: hidden;
    padding: 9px 0 0;
    h1 {
        float: left;
        height: 19px;
        width: 115px;
        margin: 1px 20px 0 0;
        a {
            background: url("images/logo.png") no-repeat scroll 0 0 transparent;
            float: left;
            height: 19px;
            text-indent: -9999px;
            width: 115px;
        }
    }
}

但出于某种原因,在第一个输入中输入'correct'并单击submit后,我仍然得到'无效'。信息。有什么建议可以解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

register-form是一个id,而不是使用#而不是.

$("#register-form form").submit(function() {

此外,我不知道$j使用$jQuery

的内容

在此处查看有效的演示http://jsfiddle.net/mEUMr/2/

确保加载jQuery并使用$(doument).ready()

答案 1 :(得分:1)

它工作正常,但是你的代码有几点(只是注意到Usman发布了类似的答案) - 请看我的版本:

http://jsfiddle.net/HWanY/

  1. 您正在使用$j()而不是$(),但是您可能有noConflict();?
  2. 您的div #register-form未关闭
  3. 您的bbp-username和其他字段未关闭div,并且为空。
  4. 甚至没有提交按钮。
  5. 最后,请确保包含jQuery。
  6. 除此之外,正如你可以在我的小提琴中看到的那样(现在也提交了一个关于提交的窗口警报),正确格式化后代码工作正常。

    你的js需要是:

    $j("#register-form form").submit(function() {
      if ($j("input:first").val() == "correct") {
        $j("#registration-notification").animate({
                height: "21px",
                opacity: 1,
                'padding-top': "8px"
            }, 800 );
        return true;
      }else{
         $j("span").text("Not valid!").show().fadeOut(1000);
         return false;
      }
    }); 
    
相关问题