通过单击按钮在字段有效时显示容器

Make a container appear when field is valid with a button click

我是 jQuery 的新手。

我正在使用一个名为 jQuery Validate 的 jQuery 插件。我尝试查看他们网站上的示例,但找不到与我的相似的内容。

我尝试了各种方法,但我无法让它工作。

想法是在用户成功填写邮政编码并单击按钮时显示数据容器。当他们按下输入键盘按钮时,它不应该出现。

$("#searchForm").validate({
    rules: {
        text: {
            required: true,
            minlength: 7
        }
    }
});

$(".trigger").click(function(e) {
    $(".even").removeClass("hide");
});
html {
    overflow-x: hidden;
}

body {
    background-image: radial-gradient(51.26% 104.44%, #313B4B 0%, #222831 100%);
    padding: 35px 30px;
}

li {
    color: #92B572;
    list-style-type: none;
    font-size: 18px;
    margin: 10px 0;
}

h1,
h2,
h4,
span {
    color: #B9E4FA;
}

h3 {
    padding-top: 130px;
    color: #FFF;
}

h1 {
    font-size: 45px;
    font-weight: light;
    line-height: 55px;
    text-align: center;
}

h2 {
    font-size: 25px;
    line-height: 55px;
    margin: 0 0 50px 0;
    text-align: center;
}

label,
p {
    color: #B9E4FA;
    font-family: "FSElliot";
    font-size: 20px;
}

input {
    background-color: #E5E5E5;
    border-radius: 8px;
    box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5), inset 0 2px 3px 0 rgba(0, 0, 0, 0.5);
    height: 67px;
    width: 100%;
    border-style: none;
}

button {
    background-color: #70BBB7;
    transition: background-color 0.5s ease;
    border-radius: 8px;
    height: 69px;
    width: 100%;
    color: #FFFFFF;
    font-weight: 700;
    line-height: 30px;
    text-align: center;
    outline-style: none;
    border-style: none;
    font-size: 18px;
}

button:hover {
    background-color: #497E7B;
}

.center-block {
    line-height: 140px;
}

input:active,
input:focus,
button:active,
button:focus {
    outline: none;
    border: none;
}

input[type=text] {
    padding: 10px 20px;
    font-size: 18px;
}

.rectangle {
    background-image: linear-gradient(180deg, #313B4B 0%, #2C3640 100%);
    opacity: 0.15;
    width: 2000px;
    height: 400px;
    z-index: -1;
    position: absolute;
    transform: rotate(20deg);
    -ms-transform: rotate(20deg);
    -webkit-transform: rotate(20deg);
    margin: 20px 0;
}

.uprnResult {
    font-weight: 700;
    letter-spacing: 3.8px;
    line-height: 50px;
    font-size: 40px;
    color: #FFF;
}

.data-container {
    text-align: center;
    margin: 35px 0;
}

.data-oval {
    background-color: #FBF6EF;
    border-radius: 100%;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.5), inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
    width: 230px;
    height: 230px;
    display: inline-block;
}

.data-rectangle {
    background-color: #3A4554;
    border-radius: 8px;
    box-shadow: 0 3px 4px 0 rgba(0, 0, 0, 0.38);
    opacity: 0.7442336309523809;
    width: 100%;
    height: 430px;
    margin-top: -100px;
    position: relative;
    z-index: -1;
}


div.data-rectangle h3 {
    display: inline-block;
    position: relative;
}

div.data-rectangle h3:after {
    position: absolute;
    content: '';
    border-bottom: 1px solid #FFF;
    width: 150px;
    transform: translateX(-50%);
    bottom: -35px;
    left: 50%;
}

div.data-rectangle h3+p {
    color: #FFFFFF;
    font-size: 120px;
    font-weight: 700;
    margin-top: 35px;
    text-align: center;
}

.rawData-headers {
    margin: 25px 0;
}

.rawData-headers h4 {
    margin: 0 0 15px 0;
}

.rawData-container {
    background-color: #3A4554;
    border-radius: 8px;
    box-shadow: 0 3px 4px 0 rgba(0, 0, 0, 0.38);
    opacity: 0.7442336309523809;
    width: 100%;
    padding: 25px 15px;
}

.hide {
    display: none;
}
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="css/styles.css">
    <script src="https://kit.fontawesome.com/d7ccf5cd0f.js"></script>
    <title>test</title>
</head>

<body>
    <section>
        <article>
            <div class="rectangle"></div>
        </article>
        <!-- Logo section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col-lg-2">
                        <a href="index.html">
                            <div class="logo"></div>
                        </a>
                    </div>
                    <div class="col">
                    </div>
                    <div class="col">
                    </div>
                </div>
            </div>
        </article>
        <!-- Headers section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col">
                    </div>
                    <div class="col-lg-6">
                        <h1>Test</h1>
                        
                    </div>
                    <div class="col">
                    </div>
                </div>
            </div>
        </article>
        <!-- Form section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col-lg-10">
                        <form id="searchForm">
                            <label>Zip Code</label>
                            <input type="text" name="text">
                        </form>
                    </div>
                    <div class="col-lg-2 center-block">
                        <button type="submit" value="submit" class="trigger">Submit!</button>
                    </div>
                </div>
            </div>
        </article>
            <!-- Data Container -->
            <div class="even hide">
                <article>
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-4">
                                <div class="data-container">
                                    <div class="data-oval">
                                    </div>
                                    <div class="data-rectangle">
                                        <h3>Data</h3>
                                        <p>4</p>
                                    </div>
                                </div>
                            </div>
                </article>
            </div>
    </section>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="js/functions.js"></script>

每当用户键入或更改它的值时,您需要检查该字段是否 valid()(使用此插件的表单中的每个字段都有该方法):

$("#searchForm input").on("keyup", function() {
  if ($(this).valid()) {
    $("#data-container").show();
  }
  else {
    $("#data-container").hide();
  }
});

您更新的代码段:

$("#searchForm").validate({
    rules: {
        text: {
            required: true,
            minlength: 7
        }
    }
});

$(".trigger").click(function(e) {
    $(".even").removeClass("hide");
});

$("#searchForm input").on("keyup", function() {
  if ($(this).valid()) {
    $("#data-container").show();
  }
  else {
    $("#data-container").hide();
  }
});
html {
    overflow-x: hidden;
}

body {
    background-image: radial-gradient(51.26% 104.44%, #313B4B 0%, #222831 100%);
    padding: 35px 30px;
}

li {
    color: #92B572;
    list-style-type: none;
    font-size: 18px;
    margin: 10px 0;
}

h1,
h2,
h4,
span {
    color: #B9E4FA;
}

h3 {
    padding-top: 130px;
    color: #FFF;
}

h1 {
    font-size: 45px;
    font-weight: light;
    line-height: 55px;
    text-align: center;
}

h2 {
    font-size: 25px;
    line-height: 55px;
    margin: 0 0 50px 0;
    text-align: center;
}

label,
p {
    color: #B9E4FA;
    font-family: "FSElliot";
    font-size: 20px;
}

input {
    background-color: #E5E5E5;
    border-radius: 8px;
    box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5), inset 0 2px 3px 0 rgba(0, 0, 0, 0.5);
    height: 67px;
    width: 100%;
    border-style: none;
}

button {
    background-color: #70BBB7;
    transition: background-color 0.5s ease;
    border-radius: 8px;
    height: 69px;
    width: 100%;
    color: #FFFFFF;
    font-weight: 700;
    line-height: 30px;
    text-align: center;
    outline-style: none;
    border-style: none;
    font-size: 18px;
}

button:hover {
    background-color: #497E7B;
}

.center-block {
    line-height: 140px;
}

input:active,
input:focus,
button:active,
button:focus {
    outline: none;
    border: none;
}

input[type=text] {
    padding: 10px 20px;
    font-size: 18px;
}

.rectangle {
    background-image: linear-gradient(180deg, #313B4B 0%, #2C3640 100%);
    opacity: 0.15;
    width: 2000px;
    height: 400px;
    z-index: -1;
    position: absolute;
    transform: rotate(20deg);
    -ms-transform: rotate(20deg);
    -webkit-transform: rotate(20deg);
    margin: 20px 0;
}

.uprnResult {
    font-weight: 700;
    letter-spacing: 3.8px;
    line-height: 50px;
    font-size: 40px;
    color: #FFF;
}

.data-container {
    text-align: center;
    margin: 35px 0;
}

.data-oval {
    background-color: #FBF6EF;
    border-radius: 100%;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.5), inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
    width: 230px;
    height: 230px;
    display: inline-block;
}

.data-rectangle {
    background-color: #3A4554;
    border-radius: 8px;
    box-shadow: 0 3px 4px 0 rgba(0, 0, 0, 0.38);
    opacity: 0.7442336309523809;
    width: 100%;
    height: 430px;
    margin-top: -100px;
    position: relative;
    z-index: -1;
}


div.data-rectangle h3 {
    display: inline-block;
    position: relative;
}

div.data-rectangle h3:after {
    position: absolute;
    content: '';
    border-bottom: 1px solid #FFF;
    width: 150px;
    transform: translateX(-50%);
    bottom: -35px;
    left: 50%;
}

div.data-rectangle h3+p {
    color: #FFFFFF;
    font-size: 120px;
    font-weight: 700;
    margin-top: 35px;
    text-align: center;
}

.rawData-headers {
    margin: 25px 0;
}

.rawData-headers h4 {
    margin: 0 0 15px 0;
}

.rawData-container {
    background-color: #3A4554;
    border-radius: 8px;
    box-shadow: 0 3px 4px 0 rgba(0, 0, 0, 0.38);
    opacity: 0.7442336309523809;
    width: 100%;
    padding: 25px 15px;
}

.hide {
    display: none;
}
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="css/styles.css">
    <script src="https://kit.fontawesome.com/d7ccf5cd0f.js"></script>
    <title>test</title>
</head>

<body>
    <section>
        <article>
            <div class="rectangle"></div>
        </article>
        <!-- Logo section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col-lg-2">
                        <a href="index.html">
                            <div class="logo"></div>
                        </a>
                    </div>
                    <div class="col">
                    </div>
                    <div class="col">
                    </div>
                </div>
            </div>
        </article>
        <!-- Headers section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col">
                    </div>
                    <div class="col-lg-6">
                        <h1>Test</h1>
                        
                    </div>
                    <div class="col">
                    </div>
                </div>
            </div>
        </article>
        <!-- Form section -->
        <article>
            <div class="container">
                <div class="row">
                    <div class="col-lg-10">
                        <form id="searchForm">
                            <label>Zip Code</label>
                            <input type="text" name="text">
                        </form>
                    </div>
                    <div class="col-lg-2 center-block">
                        <button type="submit" value="submit" class="trigger">Submit!</button>
                    </div>
                </div>
            </div>
        </article>
            <!-- Data Container -->
            <div class="even hide" id="data-container">
                <article>
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-4">
                                <div class="data-container">
                                    <div class="data-oval">
                                    </div>
                                    <div class="data-rectangle">
                                        <h3>Data</h3>
                                        <p>4</p>
                                    </div>
                                </div>
                            </div>
                </article>
            </div>
    </section>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="js/functions.js"></script>