简体   繁体   English

使用JavaScript显示/隐藏表单的某些部分。 如何一次只显示一个部分?

[英]Show/hide certain sections of a form using JavaScript. How to only show one section at a time?

Hi I have the following for set-up: 嗨,我有以下设置:

<form action="?" method="post" name="contactform">

<div class="question">
    <p><span><input type="radio" name="answer" value="Yes" onclick="showHide('one')"/></span> Yes</p>
</div>
<div id="one" class="answers" style="display:none">
    <p><input type="radio" name="answerdetail" value="Reason One" /> Reason One</p>
    <p><input type="radio" name="answerdetail" value="Reason Two" /> Reason Two</p>
    <p><input type="radio" name="answerdetail" value="Reason Three" /> Reason Three</p>
</div><!-- end answers -->

<div class="question">
    <p><span><input type="radio" name="answer" value="No" onclick="showHide('two')"/></span> No</p>
</div>
<div id="two" class="answers" style="display:none">
    <p><input type="radio" name="answerdetail" value="Reason One" /> Reason One</p>
    <p><input type="radio" name="answerdetail" value="Reason Two" /> Reason Two</p>
    <p><input type="radio" name="answerdetail" value="Reason Three" /> Reason Three</p>
    <p><input type="radio" name="answerdetail" value="Reason Four" /> Reason Four</p>
</div><!-- end answers -->

<div class="question">
    <p><span><input type="radio" name="answer" value="Not sure" /></span> Not sure</p>
</div>

When you first see the form there are only 3 check boxes visible (Yes, No, Not Sure). 当您第一次看到表单时,只能看到3个复选框(是,否,不确定)。 When you click 'Yes', the sub checkboxes for 'Yes' appear and if you click 'No', the sub checkboxes for 'No' appear. 单击“是”时,将显示“是”的子复选框,如果单击“否”,则会显示“否”的子复选框。

I would like to know if it is possible to only show one set of sub checkboxes at a time? 我想知道是否有可能一次只显示一组子复选框?

Ie if somebody checks 'Yes' the sub checkboxes for 'Yes' appear. 即如果有人检查“是”,则出现“是”的子复选框。 If they then change their mind and click 'No' the sub checkboxes for 'Yes' disappear and the sub checkboxes for 'No' appear (ie only one set of sub checkboxes is ever visible at any one time). 然后,如果他们改变主意并单击“否”,则“是”的子复选框将消失,并显示“否”的子复选框(即,任何时候只能看到一组子复选框)。

I hope this makes sense. 我希望这是有道理的。

Any help would be greatly appreciated! 任何帮助将不胜感激!

jQuery has several built in methods which should be useful for this. jQuery有几个内置的方法,应该对此有用。 You can use .hide() / .show() to toggle the visibility of the elements. 您可以使用.show() .hide() / .show()来切换元素的可见性。 You can target the correct answer section using .next() . 您可以使用.next()定位正确的答案部分。 Furthermore, to handle the 'change' event, you can use .change() , which will fire the handler every time the user selects a different option: 此外,要处理'change'事件,可以使用.change() ,每次用户选择不同的选项时都会触发处理程序:

$(document).ready(function() {
    $('input[name="answer"]').change(function() {
        $(".answers").hide();
        $('input[name="answer"]:checked')
            .closest(".question")
            .next(".answers")
            .show();
    });
});

Here's how I would approach this: 这是我如何处理这个问题:

Demo test: http://jsfiddle.net/earthdesigner/UYWx8/ 演示测试: http//jsfiddle.net/earthdesigner/UYWx8/

The easiest way I could think of, given your HTML: 考虑到你的HTML,我能想到的最简单的方法:

$('input:radio').change(
    function(){
        $('.answers').slideUp();
        $(this).closest('div').next('.answers').slideDown();
    });​

JS Fiddle demo . JS小提琴演示

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM