Onclick事件在新添加的行中不起作用

时间:2015-07-28 09:21:09

标签: javascript jquery html

这是我的代码。当我单击添加行时,它将添加下面的行。但点击事件仅在第一行中有效。

它没有在其余行中工作。

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="http://web-86d95219-b398-4432-85a8-fae716ac3a54.runnablecodesnippets.com/css/datepicker.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>


<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="http://web-86d95219-b398-4432-85a8-fae716ac3a54.runnablecodesnippets.com/js/bootstrap-datepicker.js"></script>
</head>
<body>
<div class="container">
    <div class="row clearfix">
		<div class="col-md-12 column">
			<table class="table table-bordered table-hover" id="tab_logic">
				<thead>
					<tr >
						<th class="text-center">
							#
						</th>
						<th class="text-center">
							Name
						</th>
						<th class="text-center">
							Mail
						</th>
						<th class="text-center">
							Mobile
						</th>
					</tr>
				</thead>
				<tbody>
					<tr id='addr0'>
						<td>
						1
						</td>
						<td>
						<input id = "date0" type="text" name='name0'  placeholder='Name' class="form-control"/>
						</td>
						<td>
						<input type="text" name='mail0' placeholder='Mail' class="form-control"/>
						</td>
						<td>
              					<select name="category" class="input category"><option selected>Choose </option><option >Chooser2</option></select>
						</td>
					</tr>
                    <tr id='addr1'></tr>
				</tbody>
			</table>
		</div>
	</div>
	<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>

<script>
     $(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
		var datepic = "#date" + i;
		$('#addr'+i).html("<td>"+ (i+1) +"</td><td><input id = 'date"+i+"'  name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+i+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><select class='input category'><option selected>d</option></select></td>");
		$(datepic).datepicker();
	     	 $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
	      	i++; 
 	 });
     $("#delete_row").click(function(){
    	 if(i>1){
		 $("#addr"+(i-1)).html('');
		 i--;
		 }
	 });
	$(".category").click(function(){
	alert('df');
	 });
});
  $(function() {
    var startDate = new Date(2015,2,30);
    $('#date0').datepicker('setDate',startDate);
  });
</script>
</body>
</html>

您可以签入代码段。

提前致谢。

2 个答案:

答案 0 :(得分:5)

对于动态添加的行,使用cookieJSON.Cases.mdata4添加单击事件句柄,因为当您为添加行按钮附加单击处理程序时,动态行不存在,因此您需要使用{{1}附加单击事件处理程序将事件委托给匹配的选择器。

.on()

答案 1 :(得分:2)

改变这个:

$(".category").click(function(){
    alert('df');
     });

到此:

$(document).on('click', '.category', function(){
        alert('df');
});

它被称为委托事件。

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="http://web-86d95219-b398-4432-85a8-fae716ac3a54.runnablecodesnippets.com/css/datepicker.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>


<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="http://web-86d95219-b398-4432-85a8-fae716ac3a54.runnablecodesnippets.com/js/bootstrap-datepicker.js"></script>
</head>
<body>
<div class="container">
    <div class="row clearfix">
		<div class="col-md-12 column">
			<table class="table table-bordered table-hover" id="tab_logic">
				<thead>
					<tr >
						<th class="text-center">
							#
						</th>
						<th class="text-center">
							Name
						</th>
						<th class="text-center">
							Mail
						</th>
						<th class="text-center">
							Mobile
						</th>
					</tr>
				</thead>
				<tbody>
					<tr id='addr0'>
						<td>
						1
						</td>
						<td>
						<input id = "date0" type="text" name='name0'  placeholder='Name' class="form-control"/>
						</td>
						<td>
						<input type="text" name='mail0' placeholder='Mail' class="form-control"/>
						</td>
						<td>
              					<select name="category" class="input category"><option selected>Choose </option><option >Chooser2</option></select>
						</td>
					</tr>
                    <tr id='addr1'></tr>
				</tbody>
			</table>
		</div>
	</div>
	<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>

<script>
     $(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
		var datepic = "#date" + i;
		$('#addr'+i).html("<td>"+ (i+1) +"</td><td><input id = 'date"+i+"'  name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+i+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><select class='input category'><option selected>d</option></select></td>");
		$(datepic).datepicker();
	     	 $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
	      	i++; 
 	 });
     $("#delete_row").click(function(){
    	 if(i>1){
		 $("#addr"+(i-1)).html('');
		 i--;
		 }
	 });
	$(document).on('click', '.category', function(){
	alert('df');
	 });
});
  $(function() {
    var startDate = new Date(2015,2,30);
    $('#date0').datepicker('setDate',startDate);
  });
</script>
</body>
</html>