【问题标题】:JQuery Validate AJAX > undefined has no propertiesJQuery Validate AJAX > undefined 没有属性
【发布时间】:2018-01-11 22:29:01
【问题描述】:

对不起,我还没写完就发了..

单击提交按钮后出现错误:

错误日志

TypeError: undefined has no properties
[Weitere Informationen]
jquery.validate.min.js:4:22946
a.ajax
https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js:4:22946
submitHandler
http://suedsicht-projekte.de/opus/newsletter-daten/landingpage/js/main.js:13:7
d
https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js:4:962
validate/<
https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js:4:1173
dispatch
https://code.jquery.com/jquery-3.2.1.slim.min.js:3:10499
add/q.handle
https://code.jquery.com/jquery-3.2.1.slim.min.js:3:8561

脚本在其他站点上运行。但这一次它不起作用:(

JS

$(document).ready(function(){
  $("#teilnahme").validate({
    rules: {
      inputVorname: { required: true }
    },
    messages: {
      inputVorname: { required: "Bitte tragen Sie Ihren Vornamen ein." }
    },
    submitHandler: function(form) {
      theUrl = 'teilnahme.php';
      var params = $(form).serialize();

      $.ajax ({
        type: "POST",
        url: theUrl,
        data: params,
        processData: false,
        async: false,
        success: function(result) {
          $("#sendbutton").fadeOut(100).hide(function(){
          $("#successbutton").fadeIn(100).show(function(){
          $(".result").html(data);
          });
          });
        }
      });
    }
  }); //end validate
});

验证工作。但是 submitHandler 没有

HTML

<form accept-charset="utf-8" method="post" name="teilnahme" id="teilnahme">
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputVorname">Vorname</label>
        <input type="text" class="form-control" id="inputVorname" name="inputVorname" placeholder="Vorname">
      </div>
      <div class="form-group col-md-6">
        <label for="inputNachname">Nachname</label>
        <input type="text" class="form-control" id="inputNachname" name="inputNachname" placeholder="Nachname">
      </div>
    </div>
    <div class="form-group">
      <label for="inputFirm">Firma</label>
      <input type="text" class="form-control" id="inputFirm" name="inputFirm" placeholder="Firma">
    </div>
    <div class="form-group">
      <label for="inputAddress">Address 2</label>
      <input type="text" class="form-control" id="inputAddress" name="inputAddress" placeholder="Straße">
    </div>
    <div class="form-row">
      <div class="form-group col-md-2">
        <label for="inputPLZ">Postleitzahl</label>
        <input type="text" class="form-control" id="inputPLZ" name="inputPLZ">
      </div>
      <div class="form-group col-md-6">
        <label for="inputCity">Stadt</label>
        <input type="text" class="form-control" id="inputCity" name="inputCity">
      </div>
      <div class="form-group col-md-4">
        <label for="inputState">Bundesland</label>
        <select id="inputState" name="inputState" class="form-control">
          <option selected>Baden-Württemberg</option>
          <option>...</option>
        </select>
      </div>
    </div>
    <div class="custom-control custom-radio">
      <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
      <label class="custom-control-label" for="customRadio1">2 Übernachtungen, Anreise am Vortag</label>
    </div>
    <div class="custom-control custom-radio">
      <input type="radio" id="customRadio2" name="customRadio" class="custom-control-input">
      <label class="custom-control-label" for="customRadio2">1 Übernachtung, Anreise am 27.04</label>
    </div>
    <div class="custom-control custom-radio">
      <input type="radio" id="customRadio3" name="customRadio" class="custom-control-input">
      <label class="custom-control-label" for="customRadio3">1 Übernachtung</label>
    </div>
    <div class="custom-control custom-radio">
      <input type="radio" id="customRadio4" name="customRadio" class="custom-control-input">
      <label class="custom-control-label" for="customRadio4">Ohne Übernachtung</label>
    </div>
    <button type="submit" id="sendbutton" class="btn btn-success">Teilnehmen</button>
    <button class="btn btn-success" id="successbutton" disabled="disabled"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
  </form>

我不得不解释更多... 所以我想要一个简单的表格。如果我单击提交按钮,所有信息都必须转到“teilnahme.php”,其中 php 脚本将全部导入 mySQL 数据库。 也许还有其他解决方案。我不知道。

【问题讨论】:

  • 什么不起作用?
  • 与问题无关,但您应该删除async: false。这是非常非常糟糕的做法。

标签: jquery html ajax forms


【解决方案1】:

我注意到您的错误消息中提到了 jQuery slim:

https://code.jquery.com/jquery-3.2.1.slim.min.js

jQuery slim 不包含 $.ajax() 或任何其他相关函数。

如果你想使用 ajax 功能,你需要使用完整版的 jQuery。

有关“slim”构建中包含和不包含的内容的详细信息,请参阅最新的(在撰写本文时)发行说明:https://blog.jquery.com/2017/03/20/jquery-3-2-1-now-available/

【讨论】:

  • 谢谢,我认为这是主要问题之一。但我得到了一个新的:ReferenceError: data is not defined: success/&lt;/&lt; http://suedsicht-projekte.de/opus/newsletter-daten/landingpage/js/main.js:21:11 r.speed/d.complete https://code.jquery.com/jquery-3.2.1.min.js:3:29099 i https://code.jquery.com/jquery-3.2.1.min.js:2:28012 fireWith https://code.jquery.com/jquery-3.2.1.min.js:2:28783 i https://code.jquery.com/jquery-3.2.1.min.js:3:27367 r.fx.tick https://code.jquery.com/jquery-3.2.1.min.js:3:30808 eb https://code.jquery.com/jquery-3.2.1.min.js:3:25065
  • 我删除了 async:false (下面的评论说最好删除。如果我添加它,它就起作用了:/
  • @Daniel 我认为您的线路应该是$(".result").html(result);。否则你只是写回一些其他随机数据(不确定从哪里来?)肯定?这也是为什么 data 在异步调用时未定义的原因,因为它不是响应的一部分。评论是对的,在 99.9% 的情况下没有充分的理由使用 async: false - 它只是锁定了浏览器并且很少实现任何有用的东西。此外,一些浏览器已弃用它,因此您可以期望它在未来完全停止工作。异步 ajax 是个好东西,你只需要了解回调的概念。
猜你喜欢
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
相关资源
最近更新 更多