如何用克隆生成id并将其放入文本框的值
how to generate id with clone and put it value of textbox
请帮助解决这个问题,每次用户通过按下按钮克隆行时我都需要增加 id,现在克隆工作正常但是 id = 的问题,我需要为每个克隆生成新的 id 并将其放入文本框的值 "file 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>
1st: select 使用 $clone.find('.stretchnights');
克隆中的输入
2nd: 每次点击addrow按钮时id加1 你需要在点击事件外设置一个变量 var id=0
把0改成任意数字需要从
开始
3rd: 使用 .attr('id' , newid)
更改输入 ID 并使用 .val(newid)
更改其值 你可以在只有一行
4th: 使用 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>
请帮助解决这个问题,每次用户通过按下按钮克隆行时我都需要增加 id,现在克隆工作正常但是 id = 的问题,我需要为每个克隆生成新的 id 并将其放入文本框的值 "file 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>
1st: select 使用 $clone.find('.stretchnights');
2nd: 每次点击addrow按钮时id加1 你需要在点击事件外设置一个变量 var id=0
把0改成任意数字需要从
3rd: 使用 .attr('id' , newid)
更改输入 ID 并使用 .val(newid)
更改其值 你可以在只有一行
4th: 使用 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>