javascript - 更改切换开关上的网址

标签 javascript jquery

jQuery(document).ready(function () {
    jQuery('.custom-switch').click(function(){
        var url = jQuery('.anchr').attr('href');
        var src = jQuery('.anchr').attr('data-source');
        var type = jQuery('.anchr').attr('data-target');
        if(type == 'monthly'){
            jQuery('.anchr').attr('href', src);
            jQuery('.anchr').attr('data-source', url);
            jQuery('.anchr').attr('data-target', 'yearly');
        }else{
            jQuery('.anchr').attr('href', src);
            jQuery('.anchr').attr('data-source', url); 
            jQuery('.anchr').attr('data-target', 'monthly');
        }
    });
});
<div class="switch"><span class="monthly">Billed Monthly</span> <div class="custom-switch" id="undefined"><input class="custom-switch" type="checkbox" id="custom-switch-0"><label for="custom-switch-0"></label></div> <span class="annually">Billed Annually (Save 10%) </span></div>


<ul><li class="plan__price"><a href="https://www.example.com/india/?amt=7499" data-source=https://www.example.com/india/?amt=6749" data-target="monthly" class="anchr btn btn-primary price-btn" target="_blank" rel="noopener"> Pay Now </a></li></ul>

<ul><li class="plan__price"><a href="https://www.example.com/india/?amt=7499" data-source=https://www.example.com/india/?amt=7749" data-target="monthly" class="anchr btn btn-primary price-btn" target="_blank" rel="noopener">Pay Now</a></li></ul>

我使用了两个具有不同网址的立即付款按钮,两个按钮在切换后获得相同的网址。需要不同的网址,如果我再次为第二个按钮编写相同的代码,但效果很好

jQuery(document).ready(function () {
    jQuery('.custom-switch').click(function(){
        var url = jQuery('.anchr').attr('href');
        var src = jQuery('.anchr').attr('data-source');
        var type = jQuery('.anchr').attr('data-target');
        if(type == 'monthly'){
            jQuery('.anchr').attr('href', src);
            jQuery('.anchr').attr('data-source', url);
            jQuery('.anchr').attr('data-target', 'yearly');
        }else{
            jQuery('.anchr').attr('href', src);
            jQuery('.anchr').attr('data-source', url); 
            jQuery('.anchr').attr('data-target', 'monthly');
        }
    });
});

最佳答案

对您的代码进行了一些修复:

  • 修复了您的 html,使所有引号都正确。
  • 将点击处理程序的选择器更改为 input.custom-switch,因为您的 html 中有多个此类。
  • 循环遍历 .anchr 以调整两个按钮。
  • 简化了循环内的代码。

jQuery('input.custom-switch').click(function() {
  jQuery('.anchr').each(function() {
    var $this = $(this);
    var url = $this.attr('href');
    var src = $this.attr('data-source');
    var type = $this.attr('data-target') === 'monthly' ? 'yearly' : 'monthly';

    $this.attr('href', src);
    $this.attr('data-source', url);
    $this.attr('data-target', type);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="switch">
  <span class="monthly">Billed Monthly</span>
  <div class="custom-switch" id="undefined">
    <input class="custom-switch" type="checkbox" id="custom-switch-0">
    <label for="custom-switch-0"></label>
  </div>
  <span class="annually">Billed Annually (Save 10%) </span>
</div>

<ul>
  <li class="plan__price">
    <a href="https://www.example.com/india/?amt=7499" data-source="https://www.example.com/india/?amt=6749" data-target="monthly " class="anchr btn btn-primary price-btn " target="_blank " rel="noopener "> Pay Now </a>
  </li>
</ul>

<ul>
  <li class="plan__price ">
    <a href="https://www.example.com/india/?amt=7499 " data-source="https://www.example.com/india/?amt=7749" data-target="monthly" class="anchr btn btn-primary price-btn" target="_blank" rel="noopener">Pay Now</a>
  </li>
</ul>

关于javascript - 更改切换开关上的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57997883/

相关文章:

javascript - KnockoutJS 中的嵌套可观察对象

javascript - 在ajax中将数组元素作为数据发送

javascript - 单击菜单链接时再次加载页面

javascript - 悬停在 ul 子元素上效果 ul after style

javascript - 类型错误 : Cannot find function 1. 0

javascript - 无法将 Bootstrap 折叠功能与 Angular 路由一起使用

javascript - 如何在 angularjs 中通过 npm 安装 angular-ui-bootstrap?

javascript - 如何过滤显示的数组并重新显示过滤后的列表?

javascript - 从数组中查找对象索引

javascript - jQuery 滚动到真正困惑的内容上突出显示的元素,需要帮助