如何使用克隆生成ID并将其值放在文本框中

时间:2018-09-19 19:55:07

标签: jquery clone

请帮助解决此问题,每次用户按下按钮克隆行时,我都需要增加id,现在克隆工作良好,但是id =的问题,我需要为每个克隆生成新的id并将其值设置为文本框“文件ID” 还要检查代码here jsfiddle

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>   
<script>
$(document).ready(function(){
$( ".add-row" ).click(function(){
    var $clone = $( "ul.personal-details" ).first().clone();
    $clone.append( "<button type='button' class='remove-row'>-</button>" );
    $clone.insertBefore( ".add-row" );
});
$( ".form-style-9" ).on("click", ".remove-row", function(){
    $(this).parent().remove();
});
});
</script>
</head>
<body>
<span class="form-style-9">
<ul class="personal-details">
    <table class="stretch" border="0">
        <tr>
            <td><label for="fname">File ID</label>
            <input name="fileid2[]" type="text" class="stretchnights" id="fileid2" value=""></td>       
<td>
<label for="fname">First Name</label>
<input  name="fname[]"  type="text" class="stretch" size="15"  maxlength="30" required >
            </td>
            <td>
                    <label for="lname">Last Name</label>
                    <input  name="lname[]"  type="text" class="stretch" size="15" maxlength="30" required  >
                </td>
           </tr>
    </table>
    </ul>
    <button type="button" class="add-row">+ New Client</button>
   <table width="25%" border="0"> <tr> <input type="submit" name="Submit" value="Insert"></tr></table>
  </span>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

第一个::使用$clone.find('.stretchnights');

在克隆中选择输入

第二个::每次单击添加按钮时,要将id增加一,您需要在点击事件var id=0之外设置一个变量,将0更改为您需要以任何数字开头的< / p>

第三:使用.attr('id' , newid)更改输入ID,并使用.val(newid)更改其值您可以在一行中完成这两项操作

第四:使用id++

在完成附加后将ID增加一

$(document).ready(function(){
  var id = 0;    // change 0 to the number you want to start with
  $( ".add-row" ).click(function(){
      var $clone = $( "ul.personal-details" ).first().clone();
      var $input = $clone.find('.stretchnights');
      $input.val('fileid'+id).attr('id' , 'fileid'+id)  // change fileid with any string you want 
      $clone.append( "<button type='button' class='remove-row'>-</button>" );
      $clone.insertBefore( ".add-row" );
      id++; // increase id by 1
  });
  $( ".form-style-9" ).on("click", ".remove-row", function(){
      $(this).parent().remove();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="form-style-9">
<ul class="personal-details">
    <table class="stretch" border="0">
        <tr>
            <td><label for="fname">File ID</label>
            <input name="fileid2[]" type="text" class="stretchnights" id="fileid2" value="fileid2"></td>        
<td>
<label for="fname">First Name</label>
<input  name="fname[]"  type="text" class="stretch" size="15"  maxlength="30" required >
            </td>
            <td>
                    <label for="lname">Last Name</label>
                    <input  name="lname[]"  type="text" class="stretch" size="15" maxlength="30" required  >
                </td>
           </tr>
    </table>
    </ul>
    <button type="button" class="add-row">+ New Client</button>
   <table width="25%" border="0"> <tr> <input type="submit" name="Submit" value="Insert"></tr></table>
  </span>