【发布时间】:2016-12-06 20:53:23
【问题描述】:
我有一个需要添加动态行的表。我设置了 4 行的限制,这将根据表格的使用而改变。
我很困惑为什么我的添加行不起作用。我确定它可能非常简单,但我确实需要解决这个问题。请指教解决方案
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script>
<!--<script src="addrow.js" language="Javascript" type="text/javascript"></script>-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"> </script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"> </script>
<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<script>
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
}
});
});
</script>
<script type='text/javascript'>
var counter = 1;
var limit = 4;
jQuery('a.addrow').click(function(event){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}else{
event.preventDefault();
counter++;
var newRow = jQuery('<tr><td><input type="text" name="' +
counter + '"/></td><td><input type="text" name="' +
counter + '"/></td><td><input type="text" name="' +
counter + '"/></td><td><input type="text" name="' +
counter + '"/></td><td><input type="text" name="' +
counter + '"/></td><td><input type="text" name="' +
counter + '"/></td></tr>');
jQuery('table.history').append(newRow);
}});
</script>
</head>
<html>
<body>
<style>
.ui-datepicker-calendar {
display: none;
}
</style>
<table width="100%">
<tbody>
<tr>
<td colspan="4" width="100%">
<h1>title</h1>
<p>some content</p>
</td>
</tr>
</table>
<table class="history" width="100%">
<tr><td>Dates(Months & Year)</td></tr>
<tr><td>From</td><td>To</td><td colspan="2">NAME - Company/ College/ Job Centre/ Armed Services</p>
</td><td colspan="2">Address</td></tr>
<tr>
<td>
<input name="startDate" id="startDate" class="date-picker" />
</td>
<td>
<input name="toDate" id="toDate" class="date-picker" />
</td>
<td>
<input type="text" name="first_name" />
</td>
<td>
<input type="text" name="last_name" />
</td>
<td>
<input type="text" name="first_name" />
</td>
<td>
<input type="text" name="last_name" />
</td>
</tr>
</table>
<a href="" title="" class="addrow">Add row</a>
【问题讨论】:
-
为什么要使用多个版本的
jquery.js?即jquery-1.7.js/1.4.1/jquery.js并且您的代码有效 -
这对我来说似乎很好用:jsfiddle.net/m49g53eq 你能提供一个minimal reproducible example 来证明你遇到的问题(或者,至少定义“不工作”)
-
我在小提琴上复制了它,它的工作原理。只是我只添加了一个
jQueryFiddle -
它工作正常fiddle。更新您的 jquery 版本。
-
我目前在本地主机上运行,这有影响吗?
标签: javascript jquery html