首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在location.href中点击添加变量

如何在location.href中点击添加变量
EN

Stack Overflow用户
提问于 2015-12-21 17:44:08
回答 1查看 53关注 0票数 0

让我解释一下我需要什么

首先,我想填充两个输入。如果这两个值相等,则当我单击go按钮时,它将转到另一个页面。在该页面中将有一个选项卡,如果条件为真,它将打开第一个选项卡,否则将打开第二个选项卡。

代码语言:javascript
复制
<div class="span-10 center-block text-center">
    <p>
        I am looking to <strong>borrow £</strong> 
        <input 
            type="text" 
            value="1500" 
            maxlength="4" 
            class="borrow-text-box borrow-amount number" 
            id="borrow-amount"
        > 
        over a period of 
        <input 
            type="text" 
            value="36" 
            maxlength="2" 
            class="borrow-text-box borrow-month number" 
            id="borrow-month"
        > 
        <strong>months</strong>
    </p>
    <button 
        class="button button-primary tab-select" 
        id="tab-select" 
        data-url="choice-loan.shtml"
    >GET A QUICK QUOTE</button>
</div>

<script>
    $('#tab-select').on(
        'click', 
        function() {
            var borrowamount = $('#borrow-amount').val(),
            borrowmonth = $('#borrow-month').val(),
            locationtab = $(this).attr('data-url');

            //getquote = $(this).attr('data-tab');
            if (
                borrowamount >= 200 && 
                borrowamount <= 800 && 
                borrowmonth >= 5 && 
                borrowmonth <= 9
            ) {
                location.href = locationtab;
            } else {

            }
        }
    );
</script>

选项卡代码如下

代码语言:javascript
复制
$('.tab-header li:first-child').addClass('active');
$('.tab-section .tab-content-blk:first').show();
$('.tab-header ul li').click(
    function( event ) {
        if ($(this).hasClass('active')) return false;

        var name = $(this).attr('data-tab');
        var $visible = $('.tab-section .tab-content-blk:visible');
        $('.tab-header ul li').removeClass('active');
        $(this).addClass('active');

        if ( $visible.length == 0 ) {
            showContent(name);

        } else { 
            $visible.fadeOut(
                500, 
                function() {    
                    showContent( name );
                }
            );
        }
    }
);

function showContent(name){
    $( "#" + name ).fadeIn( 500 );
}
EN

回答 1

Stack Overflow用户

发布于 2015-12-21 18:02:03

在页面中你有一个表单,你可以这样做:

代码语言:javascript
复制
$('#tab-select').on(
    'click', 
    function() {
        var destination_page = 'http://www.my-site.ext/destination-page';
        var borrowamount = $('#borrow-amount').val(),
        borrowmonth = $('#borrow-month').val(),
        locationtab = $(this).attr('data-url');

        //getquote = $(this).attr('data-tab');
        if (
            borrowamount >= 200 && 
            borrowamount <= 800 && 
            borrowmonth >= 5 && 
            borrowmonth <= 9
        ) {
            location.href = destination_page + '#tab-1';
        } else {
            location.href = destination_page + '#tab-2';
        }
    }
);

这将生成一个类似于http://www.my-site.ext/destination-page#tab-1http://www.my-site.ext/destination-page#tab-2的url,具体取决于您在脚本中执行的条件检查。

然后在目标页面中,你可以像这样访问你的"#“变量:

代码语言:javascript
复制
var active_tab = 'tab-1'; // Give a default value
if ( window.location.hash ) { // If there is a hash in the URL
    active_tab = window.location.hash; // Get the hash value
}

然后根据active_tab变量编写显示/隐藏目标选项卡的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34392731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档