javascript - jQuery 提交需要点击两次才能工作

标签 javascript jquery ajax forms

尝试提交一个 Ajax 表单,如果成功返回一个弹出窗口,但似乎只需要单击两次?

片段:

$(document).ready(function(){

  (function($){
      function processForm( e ){
          $.ajax({
              url: 'https://httpbin.org/get',
              dataType: 'text',
              type: 'get',
              contentType: 'application/x-www-form-urlencoded',
              data: $(this).serialize(),
              success: function( data, textStatus, jQxhr ){
                console.log('success' + data)
                $('.join').click(function() {
                  $('.overlay').show();
                });
                $('#video').html( data );
              },
              error: function( jqXhr, textStatus, errorThrown ){
                  console.log( errorThrown );
              }
          });

          e.preventDefault();
      }

      $('#form').submit( processForm );
  })(jQuery);

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="overlay" style="display: none;"> <!-- START OVERLAY -->

    <div class="overlay_cont">
      <div class="logo_white"></div>
      <!-- <div class="share_link">
        <input type="hidden" id="input-url" value="Copied!">
       <button class="btn-copy">Share link</button>
     </div> -->
      <div class="video">
        <video  id="v1" width="960" height="420" controls="controls"></video>
      </div>
    </div>
</div>

<div class="container">
<div id="main">
  <div class="title">
    <h1> </br> Amazing </br>live streaming</h1>
  </div>
  <div id="login">
    <h2>Join Call</h2>
    <form id="form">
      <label for="callid">Call ID:</label> </br>
      <input type="callid" name="callid"> </br>
       </br>
      <label for="passcode">Passscode:</label> </br>
      <input type="passcode" name="passcode">
      </br>
      <button class="join" type="submit">Join</button>
    </form>


  </div>
</div>

代码笔: https://codepen.io/LukaDadiani/pen/wjqXXY

最佳答案

当你第一次点击时,processForm函数被运行,因此ajax被发送,但在你的成功监听器中,你有

console.log('success' + data)    
$('.join').click(function() {
    $('.overlay').show();
});
$('#video').html( data );

因此您必须再次单击join 才能显示叠加层。您应该删除您的点击事件:

console.log('success' + data)    
$('.overlay').show();
$('#video').html( data );

关于javascript - jQuery 提交需要点击两次才能工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175996/

相关文章:

javascript - 如何将 python 中的字符串变量转换为 javascript 文字数组?

javascript - 如何将新的 React JS 前端嵌入到我现有的整体 Node JS 应用程序中?

javascript - 使用 JQuery 检测两个旋转的 div 是否发生碰撞

javascript - CSS 和 JQuery 在 Ajax 调用后不起作用

Javascript:获取子元素的宽度

javascript - 在字符串数组中查找具有重复符号的字符串

javascript - 在代码片段中加载 jQuery 并将元素添加到空白页面

jquery - 在图像 slider 中鼠标悬停时更改图像

javascript - 修改 jQuery getJSON - 添加调用和回调函数

javascript - 类似github的浏览器前进/后退滑动页面效果