【问题标题】:jQuery working not working outside the php filejQuery在php文件之外工作不工作
【发布时间】:2020-05-25 10:35:29
【问题描述】:

如果与 html 放在同一个文件上,我的 jQuery 将无法正常工作,输出将只有一个值 password,并且不会显示 email,但如果当我点击 Login 按钮时放置在不同的文件中,值 console.log(datatopost) 将在控制台上消失(如刷新)。

HTML 代码

 <!-- Login Modal -->
      <form id="loginForm" method="post">
        <div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Login to your Account</h5>
                  <!-- SIGN IN ERROR MESSAGE-->
                <div id="loginerrorMessage"></div>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                  <div class="form-group">
                      <label for="loginemail" class="sr-only">Email address</label>
                      <input type="email" class="form-control" id="loginemail" aria-describedby="emailHelp" placeholder="Enter email" autocomplete="on">
                  </div>

                  <div class="form-group">
                      <label for="loginpassword" class="sr-only">Confirm Password</label>
                      <input type="password" class="form-control" name="password" id="loginpassword" placeholder="Enter Password" autocomplete="on">
                  </div> 

                    <h6>Login as:</h6>
                    <div class="form-check form-check-inline">

                      <input type="radio" name="loginRadio" class="form-check-input" id="checkbank" value="bank">
                      <label class="form-check-label" for="checkbank">Bank</label>
                    </div>

                    <div class="form-check form-check-inline">
                      <input class="form-check-input" type="radio" name="loginRadio" id="checkcustomer" value="customer">
                      <label class="form-check-label" for="checkcustomer">Customer</label>
                    </div>

                   <div class="form-check form-check-inline">
                      <input class="form-check-input" type="radio" name="loginRadio" id="checkadmin" value="admin">
                      <label class="form-check-label" for="checkadmin">Admin</label>
                   </div>

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary mr-auto" data-target="#signupModal" data-toggle="modal" data-dismiss="modal">Register</button>

                <input type="submit" value="Login" class="btn btn-info" name="login">
                <!-- <button type="submit" class="btn btn-info">Login</button> -->

                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>
        </form>

jquery 代码

   $(document).ready(function(){
                $('#loginForm').on('submit', function(e){
                    //Stop the form from submitting itself to the server.
                    e.preventDefault();
                    let datatopost = $(this).serializeArray();
                    console.log(datatopost);

                });
            });

拜托我只是一个初学者...

【问题讨论】:

    标签: javascript php html jquery


    【解决方案1】:

    邮件没有出现是因为你没有设置name="email"

    正确的是:

     <input type="email" name="email" class="form-control" id="loginemail" aria-describedby="emailHelp" placeholder="Enter email" autocomplete="on">
    

    结果:

    [{
      name: "email",
      value: "test@test.it"
    }, {
      name: "password",
      value: "passwordtest"
    }, {
      name: "loginRadio",
      value: "bank"
    }]
    

    【讨论】:

      【解决方案2】:

      在 Chrome 中,右键单击控制台以显示带有“导航时保留日志”选项的菜单。选择此项将保留您的控制台内容。

      试一试并再次运行您的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-05
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        相关资源
        最近更新 更多