单击添加按钮添加另一个带有输入框值的单选按钮

大家好,我有一个表单,我想在其中显示多个单选按钮。因此默认情况下,我显示一个带有文本的单选按钮,并提供了“Addother”按钮选项。因此用户可以根据自己的要求在“添加选项”按钮上单击添加多个单选按钮。 。


这就是我尝试过的。


答案部分:

            <div class="custom-control custom-radio" id="add_other">

              <div id="container0">

                <input type="radio" class="custom-control-input" id="defaultUnchecked" name="defaultExampleRadios">

                <input type="search" name="" value="">

              </div>

            </div>

            <div class="custom-control custom-radio" >

              <input type="radio" class="custom-control-input" id="defaultUnchecked" name="defaultExampleRadios">

              <input type="search" name="" value="">

              <button class="remove" onclick="removeDiv(this);">X</button>

            </div>

            <div class="custom-control custom-radio">

             <input type="radio" class="custom-control-input" id="defaultUnchecked" name="defaultExampleRadios">

             <input type="search" name="" value="">

             <button class="remove" onclick="removeDiv(this);">X</button>

           </div>

           <div class="custom-control custom-radio">

             <input type="radio" class="custom-control-input" id="defaultUnchecked" name="defaultExampleRadios">

             <input type="search" name="" value="">

             <button class="remove" onclick="removeDiv(this);">X</button>

           </div>

           <div class="custom-control copy-radio" onclick="addoptions()">

              <label class="add_option" id="add_othe1r">Add More</label>

            </div>



        </div>

脚本代码:


var index = 3;


  function addoptions() {

    $('#add_other').append(`<div id="container${index}" >` + '<input type="radio" class="custom-control-input" name="defaultExampleRadios"/ >'  + `<input type="search" name="text"/>` +  "</div>");

    index ++;

  }

function removeDiv(btn){

    ((btn.parentNode).parentNode).removeChild(btn.parentNode);

  }


慕婉清6462132
浏览 129回答 2
2回答

慕桂英4014372

您需要在“add_other”容器的 id 内设置 3 个新容器。function addoptions() {&nbsp; $('#add_other').append('<div>' + '<input type="radio" class="custom-control-input" name="defaultExampleRadios">' + '<input type="search" name="" value="">' + '<button class="remove" onclick="removeDiv(this);">X</button>' + "</div>");}function removeDiv(btn) {&nbsp; ((btn.parentNode).parentNode).removeChild(btn.parentNode);}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script><div class="custom-control custom-radio" id="add_other">&nbsp; <div id="container0">&nbsp; &nbsp; <input type="radio" class="custom-control-input" name="defaultExampleRadios">&nbsp; &nbsp; <input type="search" name="" value="">&nbsp; </div>&nbsp; <div class="custom-control custom-radio">&nbsp; &nbsp; <input type="radio" class="custom-control-input"&nbsp; name="defaultExampleRadios">&nbsp; &nbsp; <input type="search" name="" value="">&nbsp; &nbsp; <button class="remove" onclick="removeDiv(this);">X</button>&nbsp; </div>&nbsp; <div class="custom-control custom-radio">&nbsp; &nbsp; <input type="radio" class="custom-control-input" name="defaultExampleRadios">&nbsp; &nbsp; <input type="search" name="" value="">&nbsp; &nbsp; <button class="remove" onclick="removeDiv(this);">X</button>&nbsp; </div>&nbsp; <div class="custom-control custom-radio">&nbsp; &nbsp; <input type="radio" class="custom-control-input" name="defaultExampleRadios">&nbsp; &nbsp; <input type="search" name="" value="">&nbsp; &nbsp; <button class="remove" onclick="removeDiv(this);">X</button>&nbsp; </div></div><div class="custom-control copy-radio" onclick="addoptions()">&nbsp; <label class="add_option" id="add_othe1r">Add More</label></div>

炎炎设计

您将 id 设置add_other为“添加选项”按钮容器。您需要删除它并将 id 重置为第一个div容器var index = 1;function addoptions() {&nbsp; $('#add_other').append(`<div id="container${index}" >` + '<input type="radio" class="custom-control-input" name="defaultExampleRadios"/>' + `<input type="search" name="text" onsearch="deleteRadio(${index})"/>` + "</div>");&nbsp; index ++;}function deleteRadio(ind) {&nbsp; $(`#container${ind}`).remove();}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script><div id="hidden_radio_buttons" class="radio_buttons" style="margin:10px;">&nbsp; <!-- Default unchecked -->&nbsp; <div class="custom-control custom-radio" id="add_other">&nbsp; &nbsp; <div id="container0">&nbsp; &nbsp; &nbsp; <input type="radio" class="custom-control-input" id="defaultUnchecked" name="defaultExampleRadios">&nbsp; &nbsp; &nbsp; <input type="search" name="" value="" placeholder="option 1" onsearch="deleteRadio(0)">&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <!-- Default unchecked -->&nbsp; <div class="custom-control copy-radio" onclick="addoptions()">&nbsp; &nbsp; <input type="radio" class="custom-control-input" name="defaultExampleRadios">&nbsp; &nbsp; <label class="add_option" id="add_othe1r">Add Option</label>&nbsp; </div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript