【问题标题】:Show/hide selected form with jQuery/CSS使用 jQuery/CSS 显示/隐藏选定的表单
【发布时间】:2013-11-13 09:55:27
【问题描述】:

我尝试根据用户的选择制作一个用于隐藏/显示表单的 jQuery 函数!

这是我的代码,你会更好地理解。

…               
<p id="joinChoice" class="parent">
    <a href="" id="mastercardChoice"><span class="overlay"></span></a>
    <a href="" id="visaChoice"><span class="overlay"></span></a>
    <a href="" id="discoverChoice"><span class="overlay"></span></a>
</p>
…


    <div class="joinForm">
        <div id="noneForm"></div>
        <div id="mastercardForm"></div>
        <div id="visaForm"></div>
        <div id="discoverForm"></div>
    </div>

span#overlay 是一个简单的复选框图像替换!

我只需要一个功能来隐藏/查看所选卡片类型的每个表单!

对于#joinChoice,我已经做了这个:

    $('#joinChoice a').click(function(){
    $(this).addClass('on')
        .siblings().removeClass('on');
  });

你能帮我更多吗

【问题讨论】:

    标签: javascript jquery html css forms


    【解决方案1】:

    这样的?

    HTML

    <p id="joinChoice" class="parent">
        <a href="javascript:void(0)" id="mastercard"><span class="overlay">Mastercard</span></a>
        <a href="javascript:void(0)" id="visa"><span class="overlay">Visa</span></a>
        <a href="javascript:void(0)" id="discover"><span class="overlay">Discover</span></a>
    </p>
    
    <div class="joinForm">
        <div id="noneForm">noneForm</div>
        <div id="mastercardForm">mastercardForm</div>
        <div id="visaForm">visaForm</div>
        <div id="discoverForm">discoverForm</div>
    </div>
    

    CSS

    .joinForm div {
        display: none;
    }
    .on {
        color: red;
        background: yellow;
    }
    

    jQuery

    $('#joinChoice a').click(function(){
        $('#joinChoice a').removeClass('on'); /* remove class from all siblings */
        $(this).addClass('on'); /* add class to active sibling */
    
        $('.joinForm div').hide(); /* hide all other forms */
        var subElement = '#' + $(this).attr("id") + 'Form';
        $( subElement ).show(); /* show active form */
    });
    

    还有一个demo

    【讨论】:

    • 感谢@LinkinTED 的帮助!
    【解决方案2】:

    更新

    我错过了重要的一行:

    e.preventDefault();
    

    这可以防止页面在点击&lt;a&gt;s 时重新加载。

    HTML:

    <p id="joinChoice" class="parent">
        <a href="" id="mastercardChoice" data-form-id="mastercardForm"><span class="overlay"></span></a>
        <a href="" id="visaChoice" data-form-id="visaForm"><span class="overlay"></span></a>
        <a href="" id="discoverChoice" data-form-id="discoverForm"><span class="overlay"></span></a>
    </p>
    

    Javascript:

    $('#joinChoice a').click(function (e) {
    
        // prevents the click event to propagate up the DOM tree
        // And thus, prevents the page to reload, in that case..
        e.preventDefault();
    
        var $this = $(e.target);
    
        // Reset others
        var $links = $this.siblings();
        $links.removeClass('on');
        $links.each(function(linkEl) {
          $( '#'+$(linkEl).data('form-id') ).hide();
        });
    
        // Activate user choice..
        $this.addClass('on')
        $('#'+$this.data('form-id')).show();
    
    });
    

    【讨论】:

    • 谢谢,工作正常!只需要在
    • 它们实际上有数百种方式!最后它们都非常相似。@LinkinTED 在下面发布了另一个。我喜欢data-form-id 的方式,因为它可以让您摆脱紧密耦合的命名约定。这样你就可以给你的表单命名,它不依赖于任何约定。
    • 如果用户选择改变,我只是一个问题!你能帮帮我吗? ?也许为 z-index css 参数添加 var +1 ?
    • 我不确定。它与您的.on CSS 类有关吗?这听起来像是与此无关的另一个问题(CSS 问题)。如果是这样,我建议您使用相关的 HTML 和 CSS 代码发布一个新问题。
    • 好的,这里是新帖子stackoverflow.com/questions/19952180/…的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多