使用JavaScript在警报中未定义电子邮件输入值

时间:2018-06-20 05:39:07

标签: javascript

我有一个包含电子邮件输入字段的表格

<form id="TypeValidation" action="" method="post" enctype='multipart/form-data'>
   <div class="form-group has-label col-sm-6">
        <label>
            Email
        </label>
        <input class="form-control" name=email" type="email" required>
        </div>
      <div class="card-footer text-center">
              <button type="submit" name="add_usr_acc" id="add_usr_acc" value="add_usr_acc" class="btn btn-primary" >Add</button>
     </div>
   </div>
</form>

问题是,在电子邮件中发出警报时,脚本代码中的内容然后未定义。如何通过用户输入电子邮件来获得价值

  <script>
    $("#TypeValidation").on('submit', (function(e) {
      var email = $(this).find('input[name="email"]').val();
      alert(email); {
        e.preventDefault();
        $.ajax({
          url: "fn_acc_submit.php?addUsraccID=" + addUsraccID,
          type: "POST",
          data: new FormData(this),
          contentType: false,
          cache: false,
          processData: false,
          dataType: "html",
          beforeSend: function() {
            $("#add_usr_acc").prop('disabled', 'disabled')
          },
          success: function(result) {
            alert(result);
            location.reload();
            if (result == '1') {
              location.replace("lst_user_account.php");

            } else {
              location.replace("add_user_account.php");

            }
          }
        });
      }
    }));
  </script>

请在我做错事的地方解决我的问题。

2 个答案:

答案 0 :(得分:3)

问题出现在您的HTML代码中的以下行:

    <input class="form-control" name=email" type="email" required>

此处name=email"无效。

将此更改为name="email"

答案 1 :(得分:2)

在您输入的字段中,其名称应为=“ email”。您忘记添加开头引号。