jquery - Bootstrap 形式 : How to dynamically create unique names for radio buttons

标签 jquery vue.js bootstrap-4

这个问题是 one 的后续问题.

这个JSfiddle体现了我想要实现的目标。

最终Solution由@Scaramouche提供

下面的函数动态构建一个“列表”,其中单选按钮每组四个。

每个组都应该有一个唯一的名称,这样就可以选择其中一个选项,同时仍然可以选择其他“组”中的其他选项。这是一个挑战,因为这个“列表”是用 Bootstrap 表单构建的。我该如何动态创建这些名称?这可以使用 Vue 或 JavaScript 来完成(没有偏好)。

HTML JSfiddle

<div id="singleQuestionModule">
    <form class="form-group">
        <div class="d-flex justify-content-center">
            <fieldset class="form-group row">
                <span class="module">
                    <legend class="col-form-legend col-sm-10"></legend>
                    <div class="input-group input-group">
                        <label id="wQuestionLabel" class="form-control-label" style="width: 540px;" for="wQuestion">Question:</label>

                    </div>
                    <div class="form-group row">
                        <div class="input-group input-group">
                            <input type="text" class="form-control" aria-label="" id="wQuestion" style="width: 540px;">
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="input-group input-group">
                            <label id="questionOptions" class="form-control-label" style="width: 540px;"
                                   for="wQuestion">Enter
                                avaible options:</label>
                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="input-group input-group">
                  <span class="input-group-addon">
                    <input type="radio" name= "q" id="option1" aria-label="">
                  </span>
                            <input type="text" class="form-control" aria-label="" style="width: 540px;">
                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="input-group input-group">
                  <span class="input-group-addon">
                    <input type="radio" name="q" id="option2" aria-label="">
                  </span>
                            <input type="text" class="form-control" aria-label="" style="width: 540px;">
                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="input-group input-group">
                  <span class="input-group-addon">
                    <input type="radio" name="q" id="option3" aria-label="">
                  </span>
                            <input type="text" class="form-control" aria-label="" style="width: 540px;">
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="input-group input-group">
                  <span class="input-group-addon">
                    <input type="radio" name="q" id="option4" aria-label="">
                  </span>
                            <input type="text" class="form-control" aria-label="" style="width: 540px;">
                        </div>
                    </div>
                </span>
                <span class="options-label"></span>
                <br>
                <div class="form-group row">
                    <div class="btn-group" data-toggle="buttons">
                        <label class="btn btn-success" id="radioAddQuestion">
                            <input type="radio" @click="counter += 1" name="options" autocomplete="off">Add Question</label>

                        <label class="btn btn-success">
                            <input type="radio" name="options" id="radioSaveTest" value="saveTest()" autocomplete="off">Save
                            Test </label>
                    </div>
                </div>
            </fieldset>
        </div>
    </form>
</div>

JavaScript

$("body").on('click', '#radioAddQuestion', function () {

    let singleQuestionModule = "singleQuestionModule";
    let question = $(".module:first").html();
    let question_label = $(".question-label:first").html();

    $(question_label).insertBefore(".options-label");
    $(question).insertBefore(".options-label");

});

最佳答案

使用计数器修改下一组的名称。还将 .html() 更改为 .clone() 以处理整个元素的副本,而不仅仅是其内容。

https://jsfiddle.net/DTcHh/60016/

关于jquery - Bootstrap 形式 : How to dynamically create unique names for radio buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634805/

相关文章:

javascript - Chrome 扩展程序模拟悬停在 Youtube 播放器上

jquery - 汉堡无法正常工作

html - 从左到右切换导航栏 Bootstrap

css - 如何使文本填充由具有负左边距的图像(旁边)创建的空白?

javascript - 在具有多个源的 iframe 上使用父文档中的 javascript(以及 jQuery 和其他库)

javascript - 更改输入/div/span 等的内容

javascript - 显示共享文件 google Drive api

vue.js - Vuejs - 路由器 child

javascript - 在 Vue 中操作完成之前,Getter 返回与输入值相同的值

javascript - VueJS 不在 Safari 中渲染