【问题标题】:Keep button in active state until clicked off保持按钮处于活动状态,直到单击关闭
【发布时间】:2020-06-18 05:56:55
【问题描述】:

这是我正在处理的页面:https://www.maisondefemmes.com/product/choose-your-own-adventure-earrings/

我希望按钮在未点击之前一直处于活动状态,以便用户可以清楚地看到他们选择的内容。

我已阅读并遵循这些说明,但无济于事:Keep button in active state until clicked upon again

注意:“按钮”曾经是我关闭的切换开关。不确定这是否会导致问题。

我已将此 JQuery 添加到我的主题中:

$('.bundled_product_optional_checkbox').click(function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active')
        } else {
            $(this).addClass('active')
        }
    });

还有这个 CSS:

.bundled_product_optional_checkbox.active {
	background-color: #cfb87c !important;
	border: 1px solid #cfb87c !important;
}

<label class="bundled_product_optional_checkbox">
  <input class="bundled_product_checkbox" type="checkbox" name="component_1592104877_bundle_selected_optional_4" value>
  " Add for "
  <span class="price">
    <span class="woocommerce-Price-amount amount">
      <span class="woocommerce-Price-currencySymbol">$</span>
    </span>
  </span>
    ::after
</label>

感谢任何答案。请善待。

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    只需创建自己的名为 active 的类

    <button class="Toggle Button">Toggle Button</button>
    

    然后创建您自己的活动类并在 css 中设置样式

    .active{
                border: none;
                background: teal;
                color: white;
            }
    

    现在在 jQuery 中,点击时切换活动类

    $('.toggle-button').click( function() {
        $('.toggle-button').toggleClass('active');
    });
    

    现在你应该有一个可以工作的拨动开关

    这是我的代码笔的链接: https://codepen.io/prabodhpanda/pen/pogNqpQ

    【讨论】:

      【解决方案2】:

      答案已更新..

      如果您不使用引导程序,则可以忽略这些类和 CDN 链接:)

      $('.bundled_product_optional_checkbox').click(function(){
              if($(this).hasClass('active')){
                  $(this).removeClass('active')
              } else {
                  $(this).addClass('active')
              }
          });
      .bundled_product_optional_checkbox.active {
      	background-color: #cfb87c !important;
      	border: 1px solid #cfb87c !important;
      }
      <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      
      
      
      <button class="btn btn-lg btn-default border bundled_product_optional_checkbox">Click me</button>

      【讨论】:

      • 感谢您的回复。我没有任何运气。我的代码有什么问题吗?我使用了错误的类'bundled_product_checkbox'吗?我是否将我的 jQuery 添加到错误的位置:/wp-content/themes/theme/js/new-file ?原始帖子的理论对我来说很有意义,但我似乎无法让元素正确。跨度>
      • @JessKumanovski 现在看看我的答案.. 我更新了我的答案
      • @maulik 对不起,我看到我的 jquery 中有错误的类。我已经更新了上述内容,但仍然没有运气。我知道它应该如何工作,这是有道理的,但是出了点问题:(
      • 我已经用我认为您要求的信息更新了我原来的问题。我没有从头开始编写代码,更新主题。
      猜你喜欢
      • 2014-07-02
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多