【问题标题】:Issue in disabling button from another form to another [duplicate]将按钮从另一种形式禁用到另一种形式的问题[重复]
【发布时间】:2014-06-17 23:19:52
【问题描述】:

我正在制作一个单页网站,并使用 jquery 进行显示和隐藏表单切换。

我想做的是在用户单击第二个链接后,它将显示前两个按钮将无法使用,而在用户单击第三个链接后,它将无法使用所有按钮。如果用户单击第一个链接,它将禁用twothree 按钮。

我的问题是点击第二个链接后,所有按钮都无法与点击后的第三个链接相同。

当前输出:http://jsfiddle.net/GZSH6/92/

脚本:

    $('#one').prop('disabled', false);
    $('#two').prop('disabled', true);
    $('#three').prop('disabled', true);


$('.one').click(function(){
    $('#one').prop('disabled', false);
    $('#two').prop('disabled', true);
    $('#three').prop('disabled', true);
});
$('.two').click(function(){
    $('#one').prop('disabled', false);
    $('#two').prop('disabled', false);
    $('#three').prop('disabled', true);
});
$('.three').click(function(){
    $('#one').prop('disabled', false);
    $('#two').prop('disabled', false);
    $('#three').prop('disabled', false);
});

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap-3


    【解决方案1】:

    首先,您不应该有具有相同 ID 的 HTML 元素。那会引起问题。其次,我建议使用 .attr()(属性)方法而不是 .prop()。

    我用这些更改修改了您的代码并且它正在工作,尽管您可能想要查看您的方法并且页面上的每个按钮只有一个副本,或者,如果您想让它们为每个 div 重复,然后设置它们的属性是固定的,因为它们似乎永远不会改变。

    我在下面的这些修改中包含了您的代码,但我也为您的 JSFiddle 分叉了这些更改。你可以在这里查看:http://jsfiddle.net/QTMR5/1/

    <a href="javascript:void(0)" class="show-page one" data-page="first">First</a>
    <a href="javascript:void(0)" class="show-page two" data-page="second">Second</button>
    <a href="javascript:void(0)" class="show-page three" data-page="third">Third</a>
    
    <div class="container">
        <div class="page first">
            <div class="row">
                <center>
                    First Page<br />
                    <button class="btnd btnd-default" id="firstone">Personal Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="firsttwo">Educational Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="firstthree">Employment Info</button>
                </center>
             </div>
        </div>
        <div class="page second hide">
            <div class="row">
                <center>
                    Second Page <br />
                     <button class="btnd btnd-default" id="secondone">Personal Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="secondtwo">Educational Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="secondthree">Employment Info</button>
                </center>
             </div>
        </div>
        <div class="page third hide">
            <div class="row">
                <center>
                    Third Page <br />
                     <button class="btnd btnd-default" id="thirdone">Personal Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="thirdtwo">Educational Info</button>&nbsp;<br />
                <button class="btnd btnd-default" id="thirdthree">Employment Info</button>
                </center>
             </div>
        </div>
    

    $(document).ready(function () {
        $('#firstone').attr("disabled", false);
        $('#firsttwo').attr('disabled', true);
        $('#firstthree').attr('disabled', true);
    
    $('.one').click(function(){
        $('#firstone').attr('disabled', false);
        $('#firsttwo').attr('disabled', true);
        $('#firstthree').attr('disabled', true);
    });
    $('.two').click(function(){
        $('#secondone').attr('disabled', false);
        $('#secondtwo').attr('disabled', false);
        $('#secondthree').attr('disabled', true);
    });
    $('.three').click(function(){
        $('#thirdone').attr('disabled', false);
        $('#thirdtwo').attr('disabled', false);
        $('#thirdthree').attr('disabled', false);
    });
    
    var value = 0, progress;
    
    function progressBar() {
        progress = setInterval(function () {
            var $bar = $('.bar');
            if (value >= 100) {
                clearInterval(progress);
                $('.progress').removeClass('active');
                $(".show-page[data-page=Profile]").trigger("click");
            } else {
                value += 10;
                $bar.width(value * 7);
            }
            $bar.text(value + "%");
        }, 800);
    };
    
    $(document).ready(function () {
        if (typeof (Storage) !== "undefined" && localStorage.getItem('pageToShow')) {
            var pageToShow = localStorage.getItem('pageToShow');
            $('.page').addClass('hide');
            $('.' + pageToShow).removeClass('hide');
        };
        $('.show-page').click(function () {
            var pageToShow = $(this).data('page');
            if (pageToShow == "progBar") {
                value = 0;
                $('.bar').width(0);
                $('.progress').addClass('active');
                progressBar();
            } else {
                clearInterval(progress);
            };
            $('.page').addClass('hide');
            $('.' + pageToShow).removeClass('hide');
            if (typeof (Storage) !== "undefined") {
                localStorage.setItem('pageToShow', pageToShow);
            };
        });
    });
    });
    

    【讨论】:

    • 我之前读过您关于刷新页面的评论。我假设您随后必须删除该评论,但如果您仍然遇到该问题,您可以通过多种方式解决它。根据您的方法,最明显的是将初始状态设置器添加到您的第一个 document.ready() 方法中,就在您为第一个 div 设置按钮的下方,您可以初始化第二个和第三个 div 的按钮。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2011-03-09
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多