简体   繁体   English

jQuery,在不同的单击上更改按钮

[英]JQuery, Changing button on different clicks

I'm setting up a registration form and using JQuery to change a button type after pressing 'next'. 我正在设置注册表格,并在按下“下一步”后使用JQuery更改按钮类型。

This currently works but I would like the button to revert in the case 'back' is pressed. 目前可以使用,但是在按下“返回”的情况下我希望按钮恢复原状。

My code is below. 我的代码如下。

      $(function () {
          $('#next').on('click', function () {
              $('.reg-page-1').animate({'left': '-105%'});
              $('.reg-page-2').animate({'left': '0px'});

              $('.btn-next-1').html('<button type="submit" class="btn btn-submit-1 btn-success btn-lg btn-block"><span class="glyphicon glyphicon-flash"></span> Submit!</button>');

          });

          $('#back').on('click', function () {
              $('.reg-page-1').animate({'left': '10%'});
              $('.reg-page-2').animate({'left': '115%'});

              $('btn-submit-1').html('<button id="next" type="button" class="btn btn-primary btn-lg btn-block" aria-expanded="false">Next </button>');
          });
       });

And the block of HTML in question is below. 下面是有问题的HTML块。

  <div class="btn-toolbar" style="width:100%;">
    <div class="btn-back-1" style="width:49%;float:left;">
        <button id="back" type="button" class="btn btn-default btn-lg btn-block" aria-expanded="false">Back </button>
    </div>
    <div class="btn-next-1" style="width:49%;float:right;">
        <button id="next" type="button" class="btn btn-primary btn-lg btn-block" aria-expanded="false">Next </button>
    </div>
  </div>

UPDATE 更新

I've updated the JS to the below 我已经将JS更新为以下内容

  $(function () {
      $('#next').on('click', function () {
          $('.reg-page-1').animate({'left': '-105%'});
          $('.reg-page-2').animate({'left': '0px'});
          $('.btn-next-1').html('<button type="submit" class="btn btn-submit-1 btn-success btn-lg btn-block"><span class="glyphicon glyphicon-flash"></span> Submit!</button>');

      });

      $('#back').on('click', function () {
                $('.reg-page-1').animate({'left': '10%'});
                $('.reg-page-2').animate({'left': '115%'});
                $('.btn-next-1').html('<button id="next" type="button" class="btn btn-primary btn-lg btn-block" aria-expanded="false">Next </button>');
            });  
});

However, I am now finding myself in a strange issue. 但是,我现在发现自己遇到了一个奇怪的问题。

The button indeed works, it changes the state of my application. 该按钮确实有效,它更改了我的应用程序的状态。

But once "back" is clicked and the button switches back to "next". 但是,一旦单击“后退”,按钮就会切换回“下一个”。 the "next" button does not seem to function or switch again to a "submit" button. “下一个”按钮似乎不起作用或再次切换到“提交”按钮。

In other words, it seems these functions are only capable of working once each. 换句话说,似乎这些功能每个只能运行一次。

You should change the second selector to be the same as the first one. 您应该将第二个选择器更改为与第一个选择器相同。 Because with the .html() you are changing the content of the container div of the nex, but that div is still there. 因为使用.html()您正在更改nex容器div的内容,但是该div仍然存在。 If you use: 如果您使用:

 $('#back').on('click', function () {
          $('.reg-page-1').animate({'left': '10%'});
          $('.reg-page-2').animate({'left': '115%'});

          $('.btn-next-1').html('<button id="next" type="button" class="btn btn-primary btn-lg btn-block" aria-expanded="false">Next </button>');
      });

it will return to the original state. 它会返回到原始状态。

Everything seems to be ok, but you dont have any selector with 'btn-submit-1', with the corrections your code for back button must be like this: 一切似乎都还可以,但是您没有任何带有'btn-submit-1'的选择器,更正后按钮的代码必须像这样:

$('#back').on('click', function () {
          $('.reg-page-1').animate({'left': '10%'});
          $('.reg-page-2').animate({'left': '115%'});            
          $('.btn-next-1').html('<button id="next" type="button" class="btn btn-primary btn-lg btn-block" aria-expanded="false">Next </button>');
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM