使用以下源代码删除单选按钮。我试过这个代码除了单选按钮之外所有元素都被删除

时间:2018-01-16 23:53:57

标签: javascript

以下是我的代码,如果有人可以解决这个问题很棒 删除单选按钮导致我的代码出现问题

        <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个元素,但删除最后一个单选按钮导致问题* /             

1 个答案:

答案 0 :(得分:0)

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()的引用,因为它似乎没有必要