javascript - 使用定义的模式生成多个动态 ID 的函数

标签 javascript jquery html

我正在尝试创建一个函数来生成具有定义模式的多个动态 ID,我该怎么做?

跟进:Vue.js: How to generare multiple dynamic ID's with a defined pattern

详细信息:

我正在创建一个动态的学校测试应用程序。单击“添加问题”按钮时, 生成具有 4 个输入字段和 4 个单选按钮的 div。每个输入和单选按钮都需要有一个 ID。 到目前为止,该函数生成了一个值,我希望它可以用作 ID。每个单选按钮都有一个 name,这样就可以在它所属的组中只选择一个选项。每个按钮 单击“添加问题”按钮(这是一个单选按钮),这个特定的“名称”用于计算按钮点击次数。在计数器的帮助下,“问题”输入字段和“选项”输入字段的动态 ID (连接到单选按钮的)以这种模式生成:

示例

测试标题-问题-选项

JavaScriptQ1   (Question)
JavaScriptQ1O1 (Option)
JavaScriptQ1O2  
JavaScriptQ1O3
JavaScriptQ1O4

由于有一个div是在测试启动的时候生成的,而id只是通过button点击一个生成的 div 不会得到它的 ID。上例中的 ID 似乎只有一个问题,但实际上是两个问题!

我目前没有使用 saveTest(),它将用于将测试保存到 mySQL 数据库中,但它可能用于解决这个问题!

1.我希望生成一个额外的“ID 组”,以便每个输入字段都获得一个 ID,但我该怎么做?

2. 当这个问题解决后,如何为单选按钮生成唯一的ID?

3. 然后我该如何使用生成的值作为 ID。我如何将它们“附加”到它们的输入字段和单选按钮?

4 有没有其他更好的方法来解决这个问题? ID 必须遵循特定模式,因为它们将保存在 一个数据库,然后用于纠正测试。

我知道这是一个相当复杂的问题,但我已尽力解释清楚。

我建议您查看 jsfiddle反而!单击“添加问题”,您将看到它是如何工作的。

var name=1;
$("body").on('click', '#radioAddQuestion', function () {
    var singleQuestionModule = "singleQuestionModule";
    var question = $(".module:first").clone()
        .find("input:text").val("").end();

    var question_label = $(".question-label:first").clone();
    $(question).find(':radio').prop('checked', false);
    $(question_label).insertBefore(".options-label:last");
    $(question).insertBefore(".options-label");
    $(question).find(':radio').attr('name', "n" + name);
// Here is where the ID value is currently generated:
    name++;

    let questionId = theTitle + "Q" + name;
    console.log(questionId);

    for (a = 1; a <= 4; a++) {
        let optionId = theTitle + "Q" + name + "O" + a;
        console.log(optionId);
    }
    console.log(name)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div id="singleQuestionModule">
    <form class="form-group">
        <div class="input-group input-group">
            <label id="wQuestionLabel" class="form-control-label" style="width: 540px;" for="wQuestion">Question:</label>

        </div>
        <!-- The Question Inputfield that needs ID-->
        <div class="input-group input-group">
            <input type="text" class="form-control" aria-label="" id="wQuestion" style="width: 540px;">
            </div>

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

        <!-- The radio buttons and option input fields that need ID's-->

        <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 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>
        <!-- The Add and Save Test Buttons-->
        <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>

最佳答案

我改了剧本。看看那个。它将为您使用过的所有控件生成动态 ID。

var name=1;
    var theTitle="online";

    $("body").on('click', '#radioAddQuestion', function () {
        name++;     
        $(".dynamicform").append(createQuestion(name));
        
    });


     $(".dynamicform").append(createQuestion(name));
    function createQuestion(name){
    	var dynamic=`<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">
                             <!-- The Question Inputfield that needs ID-->
                            
                                <input type="text" class="form-control" aria-label="" id="${theTitle + "Q" + name}" 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>
                         <!-- The radiobuttons and option inputfields that needs ID's-->

                        <div class="form-group row">
                            <div class="input-group input-group">
                      <span class="input-group-addon">
                        <input type="radio" name="${theTitle +"rg"+name}" id="${theTitle + "Q" + name + "O" + "1"}" aria-label="">
                      </span>
                                <input type="text" id="${theTitle + "Q" + name + "input" + "1"}" 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="${theTitle +"rg"+name}" id="${theTitle + "Q" + name + "O" + "2"}" aria-label="">
                      </span>
                                <input type="text" id="${theTitle + "Q" + name + "input" + "2"}" 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="${theTitle +"rg"+name}" id="${theTitle + "Q" + name + "O" + "3"}" aria-label="">
                      </span>
                                <input type="text" id="${theTitle + "Q" + name + "input" + "3"}" 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="${theTitle +"rg"+name}" id="${theTitle + "Q" + name + "O" + "4"}" aria-label="">
                      </span>
                                <input type="text" id="${theTitle + "Q" + name + "input" + "4"}" class="form-control" aria-label="" style="width: 540px;">
                            </div>
                        </div>
                    </span>
                    `;
               return dynamic;     

    }
#singleQuestionModule {
  margin-left: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div id="singleQuestionModule">
    <form class="form-group">
        <div class="d-flex justify-content-center">
            <fieldset class=" form-group row">
                <div class="dynamicform">
                  <!--Dynamic form comes here-->
                </div>
                <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>

Jsfiddle:https://jsfiddle.net/d9tfk1u6/

关于javascript - 使用定义的模式生成多个动态 ID 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49705968/

相关文章:

jquery - 悬停时的图像动画jquery

angular - 重置 Angular 7 形式的最佳实践

javascript - 如何在 Angular 7 中创建延迟加载 TreeView

javascript - 销毁Xml View sapui5

javascript - 在 jquery onscroll 中添加和删除类

javascript - 使用 Jquery 的 onClick 事件

javascript - 鼠标悬停时的 Css 从图像变为文本

javascript - 本地范围对象有时在带有 .then 的 JavaScript Promise 中工作

javascript - 第一次尝试在 jQuery 中创建可拖动的 div,但它不起作用

javascript - typeahead.js 提示和突出显示参数不适用于预取和远程