填写完所有字段后提交表单

时间:2018-05-15 08:09:21

标签: javascript jquery forms form-submit

用户点击名为“subscribe”的导航按钮后,我的标题会显示在我的标题顶部。

当用户点击此按钮时,订阅表单会在标题顶部打开 我试图让它在用户填写完所有字段之前无法提交表单。

使用我现在编写的代码,当用户点击“订阅”时,表单甚至不会打开 - 没有任何反应。

以前,一切都在运作,但是当没有填写时,表单仍会提交。

如何更改我的代码,以便在用户点击订阅按钮时再次打开订阅表单,如何才能获取该表单,以便在所有字段都已填满之前无法提交表单?

var $subscribe = $('#click-subscribe');
var $subscribeContent = $('#subscribe-content');
var $subscribeClose = $('#subscription-close');

$subscribeContent.hide();

$subscribe.on('click', function(e) {
  e.preventDefault();
  $subscribeContent.slideToggle();
});

$subscribeClose.on('click', function(e) {
  e.preventDefault();
  $subscribeContent.slideToggle();
})

var $formSubmit = $('#signup-submit'),
  $signupForm = $('.form-show'),
  $formReplace = $('#thank-you');

$formReplace.hide();

if (!$formSubmit.valid()) {
  return false;
} else {
  $formSubmit.on('click', function() {
    $signupForm.hide();
    $formReplace.show();
  })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="subscribe-content">
  <div class="subscription-signup">
    <div class="subscription-close" id="subscription-close"></div>
    <div class="email-signup">
      <p class="cat-title subscription-text">lorem ipsum</p>
      <p class="subscription-text">lorem ipsum</p>
      <p class="subscription-text">lorem ipsum</p>

      <div class="subscription-form">
        <form id="signup-form" class="form-show" name="signup-form" method="post" action="${URLUtils.url('Newsletter-SubscribeMobile')}">
          <div class="form-row salutation header">
            <label for="salutation">Title</label>
            <div class="chzn-row valid salutation">
              <select id="title" name="title" class="chzn-global-select input-select optional required">
                <option value="">--</option>
                <option value="Mr">Mr.</option>
                <option value="Mrs">Mrs.</option>
                <option value="Ms">Ms.</option>
                <option value="Miss">Miss</option>
              </select>
            </div>
          </div>

          <div class="form-row required">
            <label for="firstname">
              <span aria-required="true">First Name</span>
              <span class="required-indicator">*</span>
            </label>
            <input class="input-text required" id="firstname" type="text" name="firstname" value="" maxlength="500" autocomplete="off">
          </div>

          <div class="form-row required">
            <label for="lastname">
              <span aria-required="true">Surname</span>
              <span class="required-indicator">*</span>
            </label>
            <input class="input-text required" id="lastname" type="text" name="lastname" value="" maxlength="500" autocomplete="off">
          </div>

          <div class="form-row required">
            <label for="signup-email" style="display:none;">Email</label>
            <input class="header-signup-email" type="text" id="signup-email-header" name="signup-email" value="" placeholder="Email" />
          </div>
          <div class="form-row text-center">
            <input type="submit" name="signup-submit" id="signup-submit" class="subscribe-submit" value="Submit" />
          </div>
        </form>

        <div id="thank-you">
          <p>Thanks for subscribing!</p>
        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

你去吧

var $subscribe = $('#click-subscribe');
var $subscribeContent = $('#subscribe-content');
var $subscribeClose = $('#subscription-close');

$subscribeContent.hide();

$subscribe.on('click', function(e) {
  e.preventDefault();
  $subscribeContent.slideToggle();
});

$subscribeClose.on('click', function(e) {
  e.preventDefault();
  $subscribeContent.slideToggle();
})

var $form = $('#signup-form'),
  $signupForm = $('.form-show'),
  $formReplace = $('#thank-you');

$formReplace.hide();


$form.on('submit', function() {
  var empty = $(this).find("input, select, textarea").filter(function() {
    return this.value === "";
  });

  if (empty.length <= 0) {
    $signupForm.hide();
    $formReplace.show();
  } else {
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="click-subscribe">Show / hide form</button>
<div id="subscribe-content">
  <div class="subscription-signup">
    <div class="subscription-close" id="subscription-close"></div>
    <div class="email-signup">
      <p class="cat-title subscription-text">lorem ipsum</p>
      <p class="subscription-text">lorem ipsum</p>
      <p class="subscription-text">lorem ipsum</p>

      <div class="subscription-form">
        <form id="signup-form" class="form-show" name="signup-form" method="post" action="${URLUtils.url('Newsletter-SubscribeMobile')}">
          <div class="form-row salutation header">
            <label for="salutation">Title</label>
            <div class="chzn-row valid salutation">
              <select id="title" name="title" class="chzn-global-select input-select optional required">
                <option value="">--</option>
                <option value="Mr">Mr.</option>
                <option value="Mrs">Mrs.</option>
                <option value="Ms">Ms.</option>
                <option value="Miss">Miss</option>
              </select>
            </div>
          </div>

          <div class="form-row required">
            <label for="firstname">
              <span aria-required="true">First Name</span>
              <span class="required-indicator">*</span>
            </label>
            <input class="input-text required" id="firstname" type="text" name="firstname" value="" maxlength="500" autocomplete="off">
          </div>

          <div class="form-row required">
            <label for="lastname">
              <span aria-required="true">Surname</span>
              <span class="required-indicator">*</span>
            </label>
            <input class="input-text required" id="lastname" type="text" name="lastname" value="" maxlength="500" autocomplete="off">
          </div>

          <div class="form-row required">
            <label for="signup-email" style="display:none;">Email</label>
            <input class="header-signup-email" type="text" id="signup-email-header" name="signup-email" value="" placeholder="Email" />
          </div>
          <div class="form-row text-center">
            <input type="submit" name="signup-submit" id="signup-submit" class="subscribe-submit" value="Submit" />
          </div>
        </form>

        <div id="thank-you">
          <p>Thanks for subscribing!</p>
        </div>
      </div>
    </div>
  </div>
</div>

那它是如何运作的?

该方法实际上非常简单。

on('submit', function(){})事件将函数绑定到表单提交。因此,当我们点击提交按钮时,我们的功能会被触发。

在此功能中,我们有这个内容

var empty = $(this).find("input, select, textarea").filter(function() {
  return this.value === "";
});

if (empty.length <= 0) {
  $signupForm.hide();
  $formReplace.show();
} else {
  return false;
}

此行

var empty = $(this).find("input, select, textarea").filter(function() {
  return this.value === "";
});

指定变量empty所有inputselecttextarea字段,这些字段没有任何值,因此它们是空的。

下一步我们检查empty变量的长度,告诉我们有多少空字段。

如果等于或小于0 ,则表示没有空字段,因此我们可以提交表单。

如果不是(它高于0)则表示仍有空字段,因此我们返回false没有任何反应。