我有一个包含某些数据的表。此数据是动态的,并从数据库中获取。如果本表的一列中没有相关数据(长直线),我想阻止按钮工作。也就是说,不应该发生post(submit)操作。
我为什么想要这个?如果没有长直线(- ),那么这里就有数据。因此,相关的提交按钮不应该再次工作。为此,我编写了以下代码。然而,这是行不通的。实际上,我正在访问数据,但没有关闭按钮。
<?php
//$args = array(1,2,3,4...);
$args = array(1,2,3); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>Column 1</tr>
<tr>Column 2</tr>
<tr>Column 3</tr>
</thead>
<tbody>
<?php foreach ($args as $key => $value) { ?>
<tr>
<td>A</td>
<td><span class="related"> <?php echo !empty($value) ? $value:' — '; ?></span></td>
<form action="" method="post">
<td>
<input type="hidden" name="id">
<button type="submit" class="create-1"></button>
<button type="submit" class="create-2"></button>
<button type="submit" class="create-3"></button>
</td>
</form>
<script type="text/javascript">
jQuery(".create-1").submit(function(e){
jQuery(function() {
jQuery("span.related").each(function(index, element) {
if(jQuery(this).text() !== ' — '){
e.preventDefault();
}
});
});
});
</script>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>发布于 2022-06-05 11:39:17
尝试一下,我已经将表单移到TD中,因为它在TR中无效,正如溴啤酒中提到的那样。此外,我还更改了代码,以找到最接近的TR,然后定位.related文本。
<?php
$args = array(1, 2, 3);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>Column 1</tr>
<tr>Column 2</tr>
<tr>Column 3</tr>
</thead>
<tbody>
<?php foreach ($args as $key => $value) { ?>
<tr>
<td>A</td>
<td>
<span class="related">
<?php
echo !empty($value) ? $value : ' — ';
?>
</span>
</td>
<td>
<form action="" method="post" class="create">
<input type="hidden" name="id">
<button type="submit" class="create-1"></button>
<button type="submit" class="create-2"></button>
<button type="submit" class="create-3"></button>
</form>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
jQuery(".create button").click(function(e) {
var form = jQuery(this);
e.preventDefault();
if (form.closest("tr").find("span.related").text() !== ' — ') {
return false;
}else{
$(this).closest("form").submit();
}
});
</script>
</body>
</html>https://stackoverflow.com/questions/72506753
复制相似问题