【问题标题】:Jquery toggle form on button click按钮单击时的 Jquery 切换表单
【发布时间】:2013-04-14 18:13:20
【问题描述】:

我只想在按下按钮时显示表单。这工作正常。 当我再次单击该按钮时,我希望表单消失。有人可以解释问题是什么吗? 谢谢!

<script type="text/javascript">
$(document).ready(function() {

 $('#instantchannel').hide(); //Initially form wil be hidden.
 var numclicks = 2
  $('#instant').click(function() {
  if(numclicks%2==1){
  numclicks=++;
   $('#instantchannel').show();//Form shows on button click
}else{
numclicks=++;
 $('#instantchannel').hide();//Form shows on button click
}
   });
 });
 </script>

【问题讨论】:

  • 你有它在你的标题,$('#instant').click(function() {$('#instantchannel').toggle();});
  • 只用.toggle?
  • 显示 html 标记
  • @NikolaMitev - 为什么需要它?它会在脚本中添加什么内容?
  • numclicks=++; 呸。

标签: jquery html forms button toggle


【解决方案1】:

jQuery 有一个 toggle 方法用于这些情况:

$('#instant').click(function() {
  $('#instantchannel').toggle();
});

【讨论】:

【解决方案2】:

您在var numclicks = 2 之后缺少;。此外,当您单击该按钮时,numclicks 将不再可见。而且,你应该使用toggle()

$(document).ready(function() {
  $('#instantchannel').hide(); //Initially form wil be hidden.
  $('#instant').click(function() {
    $('#instantchannel').toggle();//Form toggles on button click
  });
});

【讨论】:

    【解决方案3】:

    您不必计算点击次数。只需使用.toggle 函数即可。

    <script type="text/javascript">
    $(document).ready(function() {
    
     $('#instantchannel').hide(); //Initially form wil be hidden.
     $('#instant').click(function() {
      $('#instantchannel').toggle();
     });
    
    });
    </script>
    

    然而,如果您想知道代码中的问题,您在var numclicks = 2 之后缺少一个;,而正确的用法++ 运算符是numclicks++;

    【讨论】:

    • 我目前将此作为我的脚本 '' 但是切换不起作用。表单出现而不是被隐藏不切换。虽然我确实让它以我原来的方式工作,但我想弄清楚如何用切换正确地做到这一点
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多