【问题标题】:Function works only with alert功能仅适用于警报
【发布时间】:2022-01-16 13:48:10
【问题描述】:

我有一个完全由 DOM 生成的表单。在该表单中,我有一个带有“更多字段”类的按钮,单击此按钮后,我想显示其他隐藏字段。但是如果没有alert(),我的脚本就无法工作。我做错了什么?

$(function() {
 $(".more-fields").on("click", function(){
  $(".other-fields").show();
 }); 
});

这不起作用。但是当我在点击之前添加 alert() 时,它正在工作:

$(function() {
 alert("xxx");
 $(".more-fields").on("click", function(){
  $(".other-fields").show();
 }); 
});

为什么没有警报就不能工作?

【问题讨论】:

    标签: click load alert live


    【解决方案1】:

    但我尝试使用它。
    没问题
    也许你应该显示你的 Html

        <!DOCTYPE html>
        <html lang="Zh-TW">
        <head>
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <meta charset="utf-8">
        <script> 
           $(function() {
         $(".more-fields").on("click", function(){
          $(".other-fields").show();
         }); 
        });
        </script>
        </head>
        <body>
        <button class="more-fields">more-fields</button >
        <div class="other-fields" style="display: none;">
        <p>other-fields</p>
        </div>
        </body>
        </html>
    

    【讨论】:

    • 我的表单在 DOM 中,你的在 HTML 中。我不能发布 HTML 或所有 JS 代码。这是很多页的代码。
    【解决方案2】:

    解决方案:

    $(function() {
     $(document).on('click', '.more-fields', function() {
      $(".other-fields").show();
     }); 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2017-12-22
      相关资源
      最近更新 更多