【问题标题】:Making one of a number of fieldsets display depending on a selected radio button根据选定的单选按钮显示多个字段集之一
【发布时间】:2012-12-03 18:26:10
【问题描述】:

我创建了a form to process user input using PHP,第一个问题要求用户选择三个单选按钮之一来填写“类型”参数 - 可用选项是“书”、“期刊”和“网站” ,代码如下所示:

<strong>Type of work:</strong>
<input type="radio" name="type" id="book" value="book" checked="checked" /> <label for="book">Book</label>
<input type="radio" name="type" id="journal" value="journal" /> <label for="journal">Journal</label>
<input type="radio" name="type" id="website" value="website" /> <label for="website">Website</label>

在页面的下方,我有三个字段集(使用&lt;fieldset&gt;),每个对应于一种类型。我希望一次只显示其中一个,具体取决于选择的单选按钮,以使页面看起来更干净。

不幸的是,我是一个 JavaScript 菜鸟,我的最后一次尝试把事情搞砸了。这些字段集已经有了 ID(boxBookboxJournalboxWebsite),尽管它们目前没有做任何特别的事情。

如果它影响任何东西,我希望输出是有效的 HTML5,并优雅地降级,如果用户禁用了 JS,则显示所有三个字段集。

任何帮助将不胜感激^^

【问题讨论】:

    标签: javascript jquery html forms


    【解决方案1】:

    我建议使用 jQuery:

    // hides the elements using jQuery (so they're visible without JavaScript)
    $('#boxBook, #boxJournal, #boxWebsite').hide();
    
    // when the radio inputs whose name is equal to "type" is changed:
    $('input:radio[name="type"]').change(function() {
        var id = this.id;
    
        // hides all the fieldset elements whose `id` starts with "box":
        $('fieldset[id^="box"]').hide();
    
        // looks for the element with the id equal to
        // `box` + upper-cased first-letter of this.id +
        // the substring from second-letter onwards of this.id
        $('#box' + id[0].toUpperCase() + id.substring(1)).show();
    });​
    

    JS Fiddle demo.

    顺便说一句,与其对radio 输入的id 执行字符串操作,不如或者使用data-* 属性来指定精确的id目标元素:

    <input type="radio" id="book" name="type" data-targets="boxBook" />
    

    并使用:

    $('#boxBook, #boxJournal, #boxWebsite').hide();
    
    $('input:radio[name="type"]').change(function() {
        var id = $(this).attr('data-targets'); // or: $(this).data('targets');
        $('fieldset[id^="box"]').hide();
        $('#' + id).show();
    });​
    

    JS Fiddle demo.


    编辑后一个代码块以满足 OP 的要求:

    $('input:radio[name="type"]').change(function() {
        $(this).siblings('input:radio[name="type"]').each(function() {
            $('#' + $(this).data('targets')).hide();
        });
        $('#' + $(this).data('targets')).show();
    }).filter(function() {
        return !this.checked;
    }).each(function() {
        $('#' + $(this).data('targets')).hide();
    });​
    

    JS Fiddle demo.

    不过,坦率地说,我认为我有点过于复杂了。但它确实有效,并且满足评论中指定的需求:

    如果默认选中其中一个单选按钮,则不会显示字段集。如果可能的话,我希望它默认预订

    【讨论】:

    • 最好将单选按钮的值更改为与 ID 完全对应。如果不出意外,它会使代码更具可读性。
    • @Blazemonger:同意;正如您评论的那样正在编辑它,尽管我使用了data-* 属性,因为value 可能完全用于其他目的=)
    • 是的,value 由处理输入的 PHP 使用。无论如何,我已经尝试过该代码,它隐藏了字段集,但似乎不再显示它们。
    • 编辑了原始代码(并发布了demo)以显示它可以正常工作。我确实编辑了代码,因为我在导致错误的字符串操作部分出现了错误。 =/
    • 太好了,效果很好!除了一件小事 - 如果默认选中其中一个单选按钮,则不会显示字段集。如果可能的话,我希望它默认预订,但这并不是什么大问题。谢谢您的帮助! (我会投票给你,但我还不能)
    【解决方案2】:

    你应该在头部的一个脚本元素中使用一个函数,大致如下

    function chooseFieldset(id) {
      document.getElementById('boxBook'   ).style.display =
          (id == 'book')   ?'display':'none';
      document.getElementById('boxJournal').style.display =
          (id == 'journal')?'display':'none';
      document.getElementById('boxWebsite').style.display =
          (id == 'website')?'display':'none';
    }
    

    然后您可以在每次单击单选按钮时调用 id,使用每个单选按钮上的属性:

    onClick="chooseFieldset(this.id);"
    

    【讨论】:

    • 我的答案是没有外部库,但我没有注意到这个问题被标记为 jQuery。 OP,你更喜欢什么?
    • 我不介意。我这样标记它是因为我安装了 JQuery,所以如果这是最好的方法,我可以使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2021-10-01
    • 1970-01-01
    • 2020-12-28
    • 2013-10-15
    相关资源
    最近更新 更多