我有一個包含某些資料的表。該資料是動態的,從資料庫中獲取。如果此表的一列中沒有相關資料(長直線 - ),我想阻止按鈕作業。也就是說,不應發生 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>
uj5u.com熱心網友回復:
試試這個,我已將表格移到 TD 內部,因為它在 TR 內部無效,如 brombeer 所述。此外,我更改了代碼以找到最接近的 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://www.uj5u.com/houduan/486262.html
