javascript - 使用 jQuery 和 css 隐藏/显示选定的表单

标签 javascript jquery html css

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

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

…               
<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>
…


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

我的 CSS 代码:

.joinForm { width: 55%; position: relative; height: 396px;}

#noneForm {
    background: url("../img/ccard-none.jpg") no-repeat scroll 0 0 #FFF;
    height:396px;
    width:616px;
    position: absolute;
    top: 0;
    display: block;
}

#mastercardForm {
    background: url("../img/mastercard-form.jpg") no-repeat scroll 0 0 #FFF;
    height:396px;
    width:616px;
    position: absolute;
    top: 0;
    display: none;
}

#visaForm {
    background: url("../img/visa-form.jpg") no-repeat scroll 0 0 #FFF;
    height:396px;
    width:616px;
    position: absolute;
    top: 0;
    display: none;
}

#discoverForm {
    background: url("../img/discover-form.jpg") no-repeat scroll 0 0 #FFF;
    height:396px;
    width:616px;
    position: absolute;
    top: 0;
    display: none;
}

和 jQuery 代码(@8y5)

$('#joinChoice a').click(function (e) {

        e.preventDefault();

    var $this = $(this);
    var i = 0;

    // 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();

}); 

最佳答案

试试这个

$('#joinChoice a').click(function (e) {

        e.preventDefault();

    var $this = $(this);
    var i = 0;

    // 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().siblings().hide();

}); 

Fiddle

$('#joinChoice a').click(function (e) {
    e.preventDefault();
    var $this = $(this);
    $(this).addClass('on').siblings().removeClass('on')
    $('#'+$this.data('form-id')).show().siblings().hide();

}); 

Fiddle

关于javascript - 使用 jQuery 和 css 隐藏/显示选定的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19952180/

相关文章:

javascript - 表单 onsubmit 无法正常工作

javascript - 为什么 JavaScript 被称为客户端技术?

javascript - 如何将带有视频的 iframe 拉伸(stretch)到其父级?

javascript - 如何在 nvd3 图例和图表之间添加间隙

javascript - 如何使用 jQuery 获取所有 ID?

html - CSS Box Shadow 不适用于 iPhone

html - 无法修复的 Bootstrap 框/边框

javascript - 在我执行 git rebase 后,应该只有一次提交

javascript - JQuery - 如何将 li 移动到 ul 中的另一个位置? (交换2里)

javascript - 获取将组复选框更改为数组的值