dttvb115151 2018-06-20 06:28
浏览 131
已采纳

动态地改变任何标签的id属性无数次

$("#add_driver").click(function() {
  $("#add_driver_section").replaceWith("<div class='wrap-input100 validate-input bg1 rs1-wrap-input100' ><span class='label-input100'>Gender</span><div class='contact100-form-radio m-t-15'> <input class='input-radio100' id='radio1' type='radio' name='type- product[]' value='male' checked='checked' > <label class='label-radio100' for='radio1'> Male </label></div><div class='contact100-form-radio'> <input class='input-radio100' id='radio2' type='radio' name='type-product[]' value='female'> <label class='label-radio100' for='radio2'> Female </label> </div></div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap-input100 validate-input bg1 rs1-wrap-input100">
  <span class="label-input100">Gender</span>
  <div class="contact100-form-radio m-t-15">
    <input class="input-radio100" id="radio1" type="radio" name="type-product[]" value="male" checked="checked">
    <label class="label-radio100" for="radio1">
      Male
    </label>
  </div>

  <div class="contact100-form-radio">
    <input class="input-radio100" id="radio2" type="radio" name="type-product[]" value="female">
    <label class="label-radio100" for="radio2">
      Female
    </label>
  </div>
</div>

I need to change the id of the radio button everytime this function is called.

There is no count to how many times the checkboxes will be dynamically created. I do not want a very complex code. Just something simple and easy to understand.

</div>
  • 写回答

2条回答 默认 最新

  • duanji9264 2018-06-20 06:50
    关注

    Just add some counter in js and use it as a number in id

    var count = 3;
    
    $("#add_driver").click(function() {
      $("#add_driver_section").append("<div class='wrap-input100 validate-input bg1 rs1-wrap-input100' ><span class='label-input100'>Gender</span><div class='contact100-form-radio m-t-15'> <input class='input-radio100' id='radio" + count + "' type='radio' name='type-product[]' value='male' checked='checked' > <label class='label-radio100' for='radio" + count + "'> Male </label></div><div class='contact100-form-radio'> <input class='input-radio100' id='radio" + ++count + "' type='radio' name='type-product[]' value='female'> <label class='label-radio100' for='radio" + count + "'> Female </label> </div></div>");
      count++;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="wrap-input100 validate-input bg1 rs1-wrap-input100">
      <span class="label-input100">Gender</span>
      <div class="contact100-form-radio m-t-15">
        <input class="input-radio100" id="radio1" type="radio" name="type-product[]" value="male" checked="checked">
        <label class="label-radio100" for="radio1">
          Male
        </label>
      </div>
    
      <div class="contact100-form-radio">
        <input class="input-radio100" id="radio2" type="radio" name="type-product[]" value="female">
        <label class="label-radio100" for="radio2">
          Female
        </label>
      </div>
    </div>
    
    <button id="add_driver">Add driver</button>
    
    <section id="add_driver_section"></section>

    I hope this is what you want to achieve. If not, then let me know.

    EDIT

    If you want those radio buttons not to act as one radio set, then you need to give them different number.

    Here is an example

    var count = 3;
    
    $("#add_driver").click(function() {
      $("#add_driver_section").append("<div class='wrap-input100 validate-input bg1 rs1-wrap-input100' ><span class='label-input100'>Gender</span><div class='contact100-form-radio m-t-15'> <input class='input-radio100' id='male-radio" + count + "' type='radio' name='type-product-" + count + "[]' value='male' checked='checked' > <label class='label-radio100' for='male-radio" + count + "'> Male </label></div><div class='contact100-form-radio'> <input class='input-radio100' id='female-radio" + count + "' type='radio' name='type-product-" + count + "[]' value='female'> <label class='label-radio100' for='female-radio" + count + "'> Female </label> </div></div>");
      count++;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="wrap-input100 validate-input bg1 rs1-wrap-input100">
      <span class="label-input100">Gender</span>
      <div class="contact100-form-radio m-t-15">
        <input class="input-radio100" id="radio1" type="radio" name="type-product[]" value="male" checked="checked">
        <label class="label-radio100" for="radio1">
          Male
        </label>
      </div>
    
      <div class="contact100-form-radio">
        <input class="input-radio100" id="radio2" type="radio" name="type-product[]" value="female">
        <label class="label-radio100" for="radio2">
          Female
        </label>
      </div>
    </div>
    
    <button id="add_driver">Add driver</button>
    
    <section id="add_driver_section"></section>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?