使用下面的源代码删除单选按钮.我已经尝试过此代码,除单选按钮外,所有元素都将被删除



下面是我的代码,如果有人可以修复它会很棒删除单选按钮导致我的代码出现问题

        <script   src="jquery-3.2.1.min.js" > </script>
        <script   src="jquery-1.4.3.js" > </script>

        <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
        <div id="p_scents">
            <p>
                <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
            </p>
        </div>



        <script>
        var max = 5;
        $(function() {
                var scntDiv = $('#p_scents');
                var i = $('#p_scents p').size() + 1;
                $('#addScnt').live('click', function() {
                    if (i <= max) {
                        $('<p><label for="p_scnts">Age Of  Person ' + i +'<input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p><br><p><label for="p_scnts"><textarea rows="4" cols="50" name="text1"></textarea></label> <a href="#" id="remScnt">Remove</a></p><p for="p_scnts[]"><input id="p_scnts" type="radio" name="gender" value="male"> Male<br><input id="p_scnts" type="radio" name="gender" value="female"> Female<br><input id="p_scnts" type="radio" name="gender" value="other"></p><a href="#" id="remScnt">Remove</a>').appendTo(scntDiv);

                        i++;
                        return false;
                    }
                });
                $('#remScnt').live('click', function() { 
                        if( i > -4 ) {
                                $(this).parents('p').remove();
                                i--;
                        }
                        return false;

                });


        });

/* 上面的代码适用于使用 Java 脚本添加的 3 个元素,但删除最后一个单选按钮会导致问题 */

var max = 5;
var i = 1;
$(function() {
  var scntDiv = $('#p_scents');
  $('#addScnt').on('click', function() {
    if (i < max + 1) {
      $('<div class="person"><p><label for="p_scnts">Age Of  Person ' + i +'<input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label></p><br><p><label for="p_scnts"><textarea rows="4" cols="50" name="text1"></textarea></label></p><p for="p_scnts[]"><input id="p_scnts" type="radio" name="gender" value="male"> Male<br><input id="p_scnts" type="radio" name="gender" value="female"> Female<br><input id="p_scnts" type="radio" name="gender" value="other">Other<br><a href="#" class="remScnt">Remove</a></p></div>').appendTo(scntDiv);
      i++;
      $('.remScnt').on('click', function() { 
        $(this).parents('div.person').remove();
        i--;
        return false;
      });
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
        <div id="p_scents">
            <p>
                <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
            </p>
        </div>

做了一些更改,这似乎对我有用。

我删除了您对size()的引用,因为它似乎没有必要

相关内容

最新更新