单击特定类以触发打开模式事件

时间:2018-01-15 01:08:49

标签: javascript php jquery html css

我该如何解决这个问题?我使用PHP来显示数据库中的数据,但我没有预见到这个错误。当我单击编辑时,模态将出现但是,当我单击其他数据的编辑按钮时,模态将不会出现。我怎样才能解决这个问题?我想要的是当我点击编辑按钮模式时会出现。我希望你可以帮我一直困扰我好几天。

编辑按钮。

Edit Button HERE

请参阅模态。

Please See Modal HERE

这是我的代码:

PHP:

echo "<div class='col-3'>";
                        echo "<div class='card'>";
                        echo "<div class='row'>";
                        echo "<div class='col-2'>";
                        echo "<h6 class='card-title'>".$row['project_name']."</h6>";
                        echo "</div>";
                        echo "<div class='col-2'>";
                        echo "<div class='card-setting'><i class='fa fa-gear'></i></div>";
                        echo "<div id='card-setting-dropdown' class='card-dropdown-content'>";
                        echo "<button class='card-dropdown-menu' id='btn-edit'>Edit Project</button>";
                        echo "<button class='card-dropdown-menu'>Delete Project</button>";
                        echo "<button class='card-dropdown-menu'>Add Task</button>";
                        echo "</div>";
                        echo "</div>";
                        echo "</div>";
                        echo "<div class='row'>";
                        echo "<div class='col-2'>";
                        echo "<label  class='project-details'>".$row['department']."</label>";
                        echo "</div>";
                        echo "<div class='col-2'>";
                        echo "<label class='project-details' style='float:right;'>Priority: <span style='color:".$color."'>".$row['priority']."</span></label>";
                        echo "</div>";
                        echo "</div>";
                        echo "<div class='pr-task-data'>";
                        echo "<div class='pr-task-summary-l'>";
                        echo "<label class='pr-task-title'>Tasks</label>";
                        echo "<p class='pr-task-details'>".$count_2."</p>";
                        echo "</div>";
                        echo "<div class='pr-task-summary-l'>";
                        echo "<label class='pr-task-title'>Completed</label>";
                        echo "<p class='pr-task-details'>".$count_3."</p>";
                        echo "</div>";
                        echo "<div class='pr-task-summary-l'>";
                        echo "<label class='pr-task-title'>In-Progress</label>";
                        echo "<p class='pr-task-details'>".$count_4."</p>";
                        echo "</div>";
                        echo "<div class='pr-task-summary-r'>";
                        echo "<label class='pr-task-title'>Not Completed</label>";
                        echo "<p class='pr-task-details'>".$count_5."</p>";
                        echo "</div>";
                        echo "</div>";
                        echo "<div class='progress'>";
                        echo "<div class='progress-bar' style='width:".$percent."%;'>";
                        echo "<label class='progress-bar-percent'>".$percent."%</label>";
                        echo "</div>";
                        echo "</div>";
                        echo "</div>";
                        echo "</div>";

                        echo "<div id='modal_2' class='modal fade'>";
                        echo "<div class='modal-dialog'>";
                        echo "<div class='modal-content'>";
                        echo "<div class='modal-header'>";
                        echo "<h6 class='modal-title'>Add Project</h6>";
                        echo "<button type='button' class='close_2'>x</button>";
                        echo "</div>";
                        echo "<div class='modal-body'>";
                        echo "<form autocomplete='off' method='POST'>";
                        echo "<input type='hidden' id='".$row['project_id']."'>";
                        echo "<div class='form-group'>";
                        echo "<label class='form-control-label'>Program</label>";
                        echo "<input type='text' placeholder='Program' name='program' id='program_2' class='form-control'>";
                        echo "</div>";
                        echo "<div class='form-group'>";
                        echo "<label class='form-control-label'>Project Name</label>";
                        echo "<input type='text' placeholder='Project Name' name='pname' id='pname' class='form-control'>";
                        echo "</div>";
                        echo "<div class='form-group'>";
                        echo "<label class='form-control-label'>Project Description</label>";
                        echo "<input type='text' placeholder='Description' name='description' id='description' class='form-control'>";
                        echo "</div>";
                        echo "<div class='form-group'>";
                        echo "<label class='form-control-label'>Department</label>";
                        echo "<select class='form-control' id='department' name='department'>";
                        echo "<option value=''>Department</option>";
                        echo "<option value='Executive Department'>Executive Department</option>";
                        echo "<option value='CCA Department'>CCA Department</option>";
                        echo "</select>";
                        echo "</div>";
                        echo "<div class='form-group'>";
                        echo "<label class='form-control-label'>Priority</label>";
                        echo "<select class='form-control' id='priority' name='priority'>";
                        echo "<option value=''>Priority</option>";
                        echo "<option value='Low'>Low</option>";
                        echo "<option value='Medium'>Medium</option>";
                        echo "<option valie='High'>High</option>";
                        echo "<option valie='High'>High</option>";
                        echo "</select>";
                        echo "</div>";
                        echo "</div>";
                        echo "<div class='modal-footer'>";
                        echo "<button type='submit' class='btn btn-update' name='update'>Update</button>";
                        echo "<button type='button' class='btn btn-secondary'>Close</button>";
                        echo "</div>";
                        echo "</form>";
                        echo "</div>";
                        echo "</div>";
                        echo "</div>";

SCRIPT:

//Edit Project Modal
var modal_edit = document.getElementById('modal_2');
var btn_edit = document.getElementById("btn-edit");
var span_edit = document.getElementsByClassName("close_2")[0];

btn_edit.onclick = function() { 
    $(modal_edit).toggleClass("modal-show");
}

span_edit.onclick = function() {
    $(modal_edit).removeClass("modal-show");    
}

$(".btn-secondary").click(function() {
    $(modal_edit).removeClass("modal-show");
});

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

  1. 在编辑按钮中添加一个公共类,如class="btnEdit other_classes"

  2. 试试此代码

    var modal_edit = document.getElementById('modal_2');
    var span_edit = document.getElementsByClassName("close_2")[0];
    
    // I assumed that your button is added dynamically
    $(document).on('click',".btnEdit",function(){
        $(modal_edit).toggleClass("modal-show");
    });
    
    span_edit.onclick = function() {
        $(modal_edit).removeClass("modal-show");    
    }
    
    $(".btn-secondary").click(function() {
        $(modal_edit).removeClass("modal-show");
    });
    
  3. 您的点击不适用于其他元素,因为您使用id为其分配事件,但id必须是唯一的。我用两个具有相同ID的元素制作样本小提琴,并尝试查看它们之间的区别..

    $("#first").click(function(){
      alert("I was clicked");
    });
    div{
    display:inline-block;
    width:100px;
    height:100px;
    border:1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="first">DIV</div>
    <div id="first">DIV</div>

    第一个只能运作..所以请改用class ..