当使用bootstrap Radio而没有预先选择选项时,即使没有选择,也会检查第一个选项的结果

When using bootstrap Radio without preselecting an option, the first option result checked even if nothing is

本文关键字:选项 有选择 检查 结果 第一个 选择 Radio bootstrap      更新时间:2023-09-26

我正在尝试使用Bootstrap Radio使用以下标记和js

<div class="form-control type">
    <div class="radio-header">Please select the type of gallery item you want to create</div>
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary">
            <input type="radio" div_class="form-inline" class="visibility_switcher" id="type_image_0" value="image" name="dgt_gallery_item[0][type]" />Image</label>
        <label class="btn btn-primary">
            <input type="radio" div_class="form-inline" class="visibility_switcher" id="type_quote_0" value="quote" name="dgt_gallery_item[0][type]" />Quote</label>
        <label class="btn btn-primary">
            <input type="radio" div_class="form-inline" class="visibility_switcher" id="type_text_0" value="text" name="dgt_gallery_item[0][type]" />Text</label>
        <label class="btn btn-primary">
            <input type="radio" div_class="form-inline" class="visibility_switcher" id="type_video_0" value="video" name="dgt_gallery_item[0][type]" />Video</label>
    </div>
</div>
    $( '#gallery_items .admin-card' ).each( function( index, element ) {
        var name = $( 'input:radio:first', this ).attr( 'name' );
        var type = $(' input[name="' + name + '"]:checked',this ).val();
        switch_visibility_of_gallery_fields_by_type( type, this );
    } );

.admin-card是一个包含许多表单输入的伙伴字段。如您所见,html中没有预选选项,但在jQuery中,在firefox中,第一个单选结果被选中。在bootstrap中也是如此,因为选择第一个选项不会触发"change"事件。这在chrome 64 Windows中是预期的。知道在firefox上用什么吗?我需要获得第一个选中的单选项的值,如果没有选中,则为undefined

Firefox保持表单控件状态(禁用和检查)跨页面加载。一个解决方法是使用autocomplete="off"。

尝试为输入字段添加autocomplete="off"

<input type="radio" div_class="form-inline" class="visibility_switcher" id="type_image_0" value="image" name="dgt_gallery_item[0][type]" autocomplete="off" />