gpt4 book ai didi

javascript - 将动态表单元素限制为最多 10 条记录

转载 作者:行者123 更新时间:2023-11-28 15:51:37 25 4
gpt4 key购买 nike

我正在尝试实现一个解决方案,用户可以引用他们的 friend 详细信息,数据将存储在数据库中

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script type='text/javascript' src='jquery121min.js'></script>

<style type='text/css'>
.extraPersonTemplate {
display:none;
}
</style>



<script type='text/javascript'>//<![CDATA[
$(function(){
$(document).ready(function () {
$('<div/>', {
'class' : 'extraPerson', html: GetHtml()
}).appendTo('#container');
$('#addRow').click(function () {
$('<div/>', {
'class' : 'extraPerson', html: GetHtml()
}).hide().appendTo('#container').slideDown('slow');

});
})
function GetHtml()
{
var len = $('.extraPerson').length;
var $html = $('.extraPersonTemplate').clone();
$html.find('[name=friendname]')[0].name="friendname" + len;
$html.find('[name=friendemail]')[0].name="friendemail" + len;
$html.find('[name=friendmob]')[0].name="friendmob" + len;
return $html.html();
}
});//]]>

</script>


</head>
<body>
<input class="span3" placeholder="Your Name" type="text" name="yourname">
<input class="span3" placeholder="Your Email" type="text" name="youremail">
<input class="span3" placeholder="Your Mobile" type="text" name="yourmob">
<div class="extraPersonTemplate">
<div class="controls controls-row">
<input class="span3" placeholder="Friend Name" type="text" name="friendname">
<input class="span3" placeholder="Friend Email" type="text" name="friendemail">
<input class="span3" placeholder="friend Mobile" type="text" name="friendmob">
</div>
</div>
<div id="container"></div>
<a href="#" id="addRow"><i class="icon-plus-sign icon-white"></i> Add another Friend</p></a>

</body>

</html>

代码工作正常点击链接时它会添加额外的记录

现在在数据库中我有以下字段

YourName, YourMobile, YourEmail, F1name,F1mob,F1email ....... f10name,f10mob,f10email

所以现在我想限制上面的代码最多只有 10 个条目,

所以我想将动态表单元素限制为仅 10 个 friend 详细信息。

提前致谢

最佳答案

尝试:

$('#addRow').click(function () {        
$('<div/>', {
'class': 'extraPerson',
html: GetHtml()
}).hide().appendTo('#container').slideDown('slow');
var len = $("#container").find(".extraPerson").length;
if(len == 10){
$('#addRow').hide();
}
});

DEMO here.

关于javascript - 将动态表单元素限制为最多 10 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com