【问题标题】:Multiple buttons on an html page to reveal text step by step rather than simultaneously?html页面上的多个按钮逐步而不是同时显示文本?
【发布时间】:2013-09-18 17:48:59
【问题描述】:

尝试创建一个包含大约 7 个问题的列表,并单击每个问题旁边的答案按钮。 唯一的问题是当我单击一个按钮时,它会运行整个 js 代码并显示所有答案,而不仅仅是那个特定的答案。

这是我的代码

<table>
  <tr>
    <td>Question 1 </td>
    <td class = "aligncenter">
      <form action="javascript:void(0);">

        <div id="reveal-space"> 
          <input type="submit" name="b1" value="1" class="submitbutton aligncenter"/>

        </div>
      </form>

      <script>

      $("form").submit(function() {

        $("#reveal-space").text("Answer 1").show();
          return true;

        });
      </script>

    </td>
  </tr>

  <tr>
    <td>Question 2</td>
    <td class = "aligncenter">
      <form action="javascript:void(0);">

        <div id="reveal-space1"> 
          <input type="submit" name="b2" value="2" class="submitbutton aligncenter"/>

        </div>
      </form>

      <script>

      $("form").submit(function() {

        $("#reveal-space1").text("Answer 2").show();

          return true;

        });
      </script>
    </td>
  </tr>

  <tr>
    <td>Question 3</td>
    <td class = "aligncenter">
      <form action="javascript:void(2);">

        <div id="reveal-space2"> 
          <input type="submit" name="b3" value="3" class="submitbutton aligncenter"/>

        </div>
      </form>

      <script>

      $("form").submit(function() {

        $("#reveal-space2").text("Answer 3").show();

          return true;

        });
      </script>

    </td>
  </tr>

  <tr>
    <td>Question 4</td>
    <td class = "aligncenter">
      <form action="javascript:void(0);">

        <div id="reveal-space3"> 
          <input type="submit" name="b4" value="4" class="submitbutton aligncenter"/>

        </div>
      </form>

      <script>

      $("form").submit(function() {

        $("#reveal-space3").text("Answer 4").show();


          return true;

        });
      </script>
    </td>
  </tr>
</table>

我该如何修复它以使单击“1”按钮时不会向其他按钮显示文本? 我可能需要采取完全不同的方法

提前感谢您的帮助

【问题讨论】:

  • 在此处修复您的代码格式,我们无法阅读!
  • 看起来这是您正在使用的伪代码表示,因此我假设您将多个匿名函数绑定到表单上的提交事件。您可以尝试绑定到特定按钮的点击事件...如果您可以提供您的标记 html 或示例以及格式化的 Javascript,我们可能会得到更具体的答案。
  • 我使用标准的 jquery-1.7.1.min.js

标签: javascript html button text


【解决方案1】:

您的表单需要唯一 ID,因为当您设置表单提交功能时,$("form") 会选择您的所有表单。

尝试改变

  <form action="javascript:void(0);">

    <div id="reveal-space"> 
      <input type="submit" name="b1" value="1" class="submitbutton aligncenter"/>

    </div>
  </form>

  <script>

  $("form").submit(function() {

    $("#reveal-space").text("Answer 1").show();
      return true;

    });
  </script>

  <form id="form1" action="javascript:void(0);">

    <div id="reveal-space"> 
      <input type="submit" name="b1" value="1" class="submitbutton aligncenter"/>

    </div>
  </form>

  <script>

  $("#form1").submit(function() {

    $("#reveal-space").text("Answer 1").show();
      return true;

    });
  </script>

变化是 id="form1" 和 $("#form1")

【讨论】:

  • 我试过了,但是当我更改不同按钮“form1、form2、form3”的表单名称时,单击按钮没有任何作用。
  • 跟进问题,如果我想让问题 1 的按钮在单击问题 2 的按钮后重新显示,我该怎么做。我尝试使用“.hide()”,但这会在没有将按钮带回来的情况下拿走答案。
  • 那是因为按钮被移除了。当您调用 .text("some text") 时,它会将标签内的内容替换为文本。所以你的输入永远消失了。要绕过它,您需要在显示空间之外进行输入。单击按钮时,除了您当前的代码之外,还有 $("#form1 input[type=submit]").hide()。
【解决方案2】:

略有不同的方法:您可以在表单元素中的隐藏 div 中加载所有答案,并通过查找与调用提交函数的表单相关的这些 div 来选择性地显示这些 div,以便其他休眠表单不会被触发。

    $("form").submit(function() {
        $(this).find("#answer").show();
        $(this).find("#reveal-space").hide();
    });

好处:你不需要唯一的ID,你只需要一个提交函数,文本不必包含在jQuery中。

jsFiddle

【讨论】:

    【解决方案3】:
    <table id="QnA">
     <tr>
      <td>Question 1</td>
      <td class = "aligncenter">
       <input type="button" value="Show Answer" data-answer="Answer goes here" class="submitbutton aligncenter"/>
      </td>
     </tr>
    </table>
    
    <script type="text/javascript">
    $("#QnA :input").on("click", function() {
     $(this).parent().html($(this).data("answer"));
    });
    </script>
    

    您不需要表单或任何提交,只需使所有内容都具有自引用性(实际上您可能甚至不需要该表上的 ID,但我不知道您页面的其余部分是什么样的) .

    【讨论】:

    • 这很好用,除非我更改表 ID。我更改了#QNA 以匹配我正在使用的表的ID,但这似乎不起作用。 QnA 是某种预定义的类吗?
    • 它是区分大小写的,你需要在第 11 行的任何东西的 ID 前面加上 # 号,让 jQuery 知道它正在寻找一个 id 为你的东西的东西-使用。
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2019-02-05
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多