这里的index.php的代码:javascript如何区分一个窗体的另一个按钮?
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Band</td>
<td>Indx</td>
<td>Send</td>
</tr>
<tr>
<td>Bruce</td>
<td>Iron Maiden</td>
<td>95</td>
<td>
<form action="" class="form" method="POST">
<input class="name" name="name" type="hidden" value="Bruce">
<input class="band" name="band" type="hidden" value="Iron Maiden">
<input class="indx" name="indx" type="hidden" value="95">
<button class ="send" type="submit">SEND<button>
</form>
</td>
</tr>
<tr>
<td>James</td>
<td>Metallica</td>
<td>90</td>
<td>
<form action="" class="form" method="POST">
<input class="name" name="name" type="hidden" value="James">
<input class="band" name="band" type="hidden" value="Metallica">
<input class="indx" name="indx" type="hidden" value="90">
<button class ="send" type="submit">SEND<button>
</form>
</td>
</tr>
</table>
<div class="result"></div>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script>
$(function(){
$('.form').submit(function(){
$.ajax({
data: $('.form').serialize(),
type: 'POST',
url: 'remote.php',
success: function(data){
$('.result').html(data);
}
});
return false;
});
});
</script>
</body>
</html>
这里remote.php
<?php
$name = $_POST['name'];
$band = $_POST['band'];
$indx = $_POST['indx'];
$up = $indx * 2;
$output = '
<div id="rock-modal" class="modal fade in" role="dialog" tabindex="-1" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">
<i class="fa fa-files-o"></i>
Rock Modal
</h4>
</div>
<div class="modal-body">
The band '.$band.' has an up index of '.$up.'!!!
</div><!-- /modal-body -->
</div><!-- /modal-content -->
</div><!-- /modal-dialog modal-lg -->
</div><!-- /modal fade in-->
<script type="text/javascript">
$("#rock-modal").modal("show");
</script>';
echo $output;
当我只有一个行的表(例如:铁娘子)我收到的代码具有相应信息的引导模式。当我在表格中有多行时(例如Iron Maiden和Metallica),当我点击第一行的按钮时,我只收到最后一行(本例中为Metallica)的结果。如果我插入另一行并单击第一个或第二个按钮,我会始终收到最后一行的信息。
我该如何说javascript到底是什么按钮?
确保两个按钮都有唯一的名称或ID ... – RichardBernards 2014-12-05 12:03:55
为每个按钮使用唯一的ID – 2014-12-05 12:05:26
您可以为每个按钮$(“。send1”)分配类,click(function(){// do button这里的行动}); – Dibish 2014-12-05 12:15:16