【问题标题】:Javascript function only works on the first row only inside my modalJavascript函数仅适用于我的模态内的第一行
【发布时间】: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">&times;</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


【解决方案1】:

正如我上面提到的,你应该使用类选择器,并且我稍微改进了你的验证

$('.password').keyup(function() {
  $("#submit").prop('disabled', checkStrength($(this).val()) === false);
});

$('.confirm_password').blur(function() {
  var arePasswordTheSame = $('.password').val() !== $(this).val();
  $('.popover-cpassword').toggleClass('hide', !arePasswordTheSame);
  $("#submit").prop('disabled', !arePasswordTheSame)
});

$('.username').blur(function() {
  var regex = /\`|\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s/;
  var isUserNameCorrect = $(this).val().match(regex);
  $('.popover-username').toggleClass('hide', !isUserNameCorrect);
  $(".submit").prop('disabled', !isUserNameCorrect);
  $('.sign-up').prop('disabled', !isUserNameCorrect);
})

$('.contact_number').blur(function() {
  var regex = /^[(9)][(\d+)]{9}$/;
  var isNumberCorrect = $(this).val().match(regex)
  $('.popover-cnumber').toggleClass('hide', !isNumberCorrect);
  $(".submit").prop('disabled', !isNumberCorrect);
});

此外,您还需要将当前容器添加到所有类 seelctors。更多信息请参考API

【讨论】:

  • 谢谢先生!我按照你说的做了,也改变了我的 javascript 代码。除了 API 之外。虽然,当我打开我的第一个编辑模式并添加一个特殊字符然后关闭模式然后打开另一个编辑模式时,来自第一个模式的警报仍然存在。这是因为没有您正在使用的 API 吗?
  • 是的,假设您的第一个模态具有“模态”类。然后,您应该在每个验证函数中使用 $('.modal:visible').find('.your_class').toggleClass。主要想法是您应该始终引用您当前打开的模式对话框并只为它做所有事情。
  • 先生,您能帮我解决我最近遇到的问题吗?
  • 你最近的问题是什么?
【解决方案2】:

这种方法只有在你的 php 代码生成一个时才有效

每个帐户,将它们全部放在 html 中。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2022-01-04
    • 2023-01-14
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多