[英]Making table cells clickable
I currently have a table of about 30 rows and I would like to make <td>
clickable in each case: 我目前有一个约30行的表,我想在每种情况下都使<td>
可点击:
<tr height="100px" align="center">
<?php do { ?>
<td style="background-color: <?php echo $row_dd1['colour']; ?>;">
<form action="pay.php?id=<?php echo $row_dd1['dNo']; ?>&user=<?php echo $username; ?>" method="post">
<input type="hidden" id="<?php echo $row_dd1['dNo']; ?>"><input type="hidden" value="<?php echo $username; ?>">
<button type="submit" class="link" id="t<?php echo $row_dd1['dNo']; ?>"><span><?php echo $row_dd1['dNo']; ?></span></button></form>
</td>
<?php } while ($row_dd1 = mysql_fetch_assoc($dd1)); ?>
</tr>
How do you make the table cell clickable? 如何使表格单元格可点击? I would like it to have the same link as the form action that I have used which is: 我希望它与我使用的表单操作具有相同的链接:
<form action="pay.php?id=<?php echo $row_dd1['dNo']; ?>&user=<?php echo $username; ?>" method="post">
This is a perfect example of where to use .delegate()
, like this: 这是在哪里使用.delegate()
的完美示例,如下所示:
$("#myTableID").delegate("td", "click", function() {
$(this).find("form").submit();
});
.delegate()
attaches one handler to the <table>
, rather than n
handlers, one per cell. .delegate()
将一个处理程序附加到<table>
,而不是n
处理程序,每个单元格一个。 If you just want to navigate to the URL, it would look like: 如果您只想导航到该URL,则如下所示:
$("#myTableID").delegate("td", "click", function() {
window.location.href = $(this).find("form").attr("action");
});
What about jQuery's click-event ? jQuery的点击事件如何?
$('td').click(function () {
$('form', this).submit();
return false;
});
This submits the form inside the clicked <td>
. 这将在单击的<td>
提交表单。
To actually submit the form: 实际提交表格:
$('td > form').each(function(i,e){
var form = $(this);
var cell = form.parent();
if(cell.is('td')){
cell.click(function(){
form.submit();
});
}
});
If you jsut want to follow the link instead, jsut modify the form.submit()
part to change window.location.href
with form.attr('action')
. 如果jsut想要跟随链接,则form.submit()
修改form.submit()
部分,以将form.attr('action')
更改为window.location.href
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.