Сделайте так, чтобы контейнер отображался, когда поле действительно с помощью нажатия кнопки - PullRequest
0 голосов
/ 28 июня 2019

Я новичок в jQuery.

Я использую плагин jQuery под названием jQuery Validate. Я попытался посмотреть примеры на их сайте, но не смог найти ничего похожего на мой.

Я пробовал разные методы, но не смог заставить его работать.

Идея состоит в том, чтобы показать контейнер данных, когда пользователь успешно заполнил свой почтовый индекс, и нажать на кнопку. Он не должен появляться, когда они нажимают кнопку ввода клавиатуры.

$("#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>

1 Ответ

0 голосов
/ 28 июня 2019

Вам нужно проверять, является ли поле 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...