猿问

如何为ajax请求中的每个表单提供另一个“警报”?

  let getLoginPassSystem = function (getPassForgotSystem, getLoginCheckSystem) {

  $(document).ready(function () {

    $('#login,#lostpasswordform,#register').submit(function (e) {

      e.preventDefault();

      $.ajax({

        type: "POST",

        url: 'http://www.virtuelles-museum.com.udev/spielelogin/logsystem.php',

        data: $(this).serialize(),

        success: function (response) {

          var data = JSON.parse(response);


          if (data.success == "accepted") {

            document.getElementById('inner').innerHTML = 'Herzlich Willkommen';


            // location.href = 'index.php';

          } else {

            alert('Ungültige Email oder Password!');

          }

        }

      });

    });

  })

}

好吧,我想为每个表单(#login,#lostpasswordform,#register)都有一个不同的“警报”。实际上可能吗?


慕丝7291255
浏览 118回答 2
2回答

RISEBY

您可以在每个 div 标签中将警报消息保存为数据属性。例如:<div id="login" data-msg="message1"></div><div id="lostpasswordform" data-msg="message2"></div><div id="register" data-msg="message3"></div>// then you can invoke them like thislet getLoginPassSystem = function (getPassForgotSystem, getLoginCheckSystem) {&nbsp; $(document).ready(function () {&nbsp; &nbsp; $('#login,#lostpasswordform,#register').submit(function (e) {&nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; let current_form = $(this);&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; url: 'http://www.virtuelles-museum.com.udev/spielelogin/logsystem.php',&nbsp; &nbsp; &nbsp; &nbsp; data: $(this).serialize(),&nbsp; &nbsp; &nbsp; &nbsp; success: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = JSON.parse(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data.success == "accepted") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('inner').innerHTML = 'Herzlich Willkommen';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // location.href = 'index.php';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(current_form.attr('data-msg'));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; })}

米琪卡哇伊

看来你可以简单地检查一下e.target- 每个表格都会有所不同。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答