【发布时间】:2019-10-16 05:12:19
【问题描述】:
我正在使用 javascript 函数进行输入验证,它在模态中使用。我点击第一行的编辑按钮,javascript函数运行良好。但是当我点击第二行和后续行的编辑按钮时,它不起作用。
我不确定问题是模态内部的数据还是模态外部的数据
我尝试在输入字段中添加一个类名并将javascript中的调用函数从“#name”更改为“.name”,但它不起作用。
我也试过showed.bs函数
// 这是我的表格代码(部分)
<?php
if ($result = mysqli_query($conn, $sql_accounts)) {
while ($row=mysqli_fetch_array($result)) {
?>
<tr>
<form action="update.php" method="POST">
<td>
<?php echo $row["username"]; ?> </td>
<td>
<?php echo $row["firstname"]; ?> </td>
<td> .....
<td>
<?php echo $row["status"]; ?> </td>
<td>
<?php
if ($row["status"] == "Deactivated") {
?>
<a href="update.php?activate=<?php echo $row['accountid']; ?>" class="btn btn-info"> Activate </a>
<?php
} elseif ($row["status"] == "Active") {
?>
<a href="update.php?deactivate=<?php echo $row['accountid']; ?>" class="btn btn-warning"> Deactivate </a>
<?php
} ?>
<a href="#edit<?php echo $row['accountid']; ?>" data-toggle="modal" class="btn btn-success" data-toggle="modal">Edit</a>
// 这是我的模态代码(部分)
<div id="edit<?php echo $row['accountid']; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Account</h4>
</div>
<div class="modal-body">
<form method="POST" action="editaccount.php">
<div class="form-group">
<input type="hidden" name="edit_item_id" value="<?php echo $row['accountid']; ?>">
<label>Username</label>
<span id="popover-username" class="pun hide pull-right block-help"><i class="fa fa-info-circle text-danger" aria-hidden="true"></i> Username must not contain any special characters</span>
<input type="text" id="username" name="username" class="username form-control" value="<?php echo $row['username']; ?>" placeholder="Enter Username">
//这是我的javascript函数
<script>
$(document).ready(function() {
$('#password').keyup(function() {
var password = $('#password').val();
if (checkStrength(password) == false) {
$("#submit").attr('disabled', 'disabled');
}
});
$('#confirm_password').blur(function() {
if ($('#password').val() !== $('#confirm_password').val()) {
$('#popover-cpassword').removeClass('hide');
$("#submit").attr('disabled', 'disabled');
} else {
$('#popover-cpassword').addClass('hide');
$("#submit").removeAttr('disabled');
}
});
$('#username').blur(function() {
var regex = /\`|\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s/;
if ($('#username').val().match(regex)) {
$('#popover-username').removeClass('hide');
$("#submit").attr('disabled', 'disabled');
} else {
$('#popover-username').addClass('hide');
$('#sign-up').attr('disabled', false);
$("#submit").removeAttr('disabled');
}
})
$('#contact_number').blur(function() {
var regex = /^[(9)][(\d+)]{9}$/;
if ($('#contact_number').val().match(regex)) {
$('#popover-cnumber').addClass('hide');
$("#submit").removeAttr('disabled');
} else {
$('#popover-cnumber').removeClass('hide');
$("#submit").attr('disabled', 'disabled');
}
});
$('#password').keyup(function() {
var password = $('#password').val();
if (checkStrength(password) == false) {
$("#submit").attr('disabled', 'disabled');
}
});
一旦输入错误,应该会出现一条消息,例如我在用户名字段中输入 # 应该有一条消息用户名必须包含任何特殊字符,其中 javascript 函数从模态中的跨度代码中删除了类隐藏
【问题讨论】:
-
您将验证逻辑绑定到 id,在这种情况下,您将获得此选择器的第一个有效输入。你应该把你所有的东西都绑定到类选择器上。
-
说每一行数据都有一个模态是否正确?
-
@dganenco 谢谢!那么我应该将所有'#'更改为'。'并在我的类名中添加一个标识符,如 class="username form-control"??
-
@RandyCasburn 是的,因为我表中的每一行都有一个编辑按钮
-
是的,这正是您需要做的。此外,我将在几分钟内为您的验证提供一些改进。
标签: javascript php html modal-dialog