使用动态值显示隐藏按钮jquery

时间:2017-05-14 21:19:59

标签: php jquery button mysqli

我在index.php中有这段代码:

 <tbody>
        <?php foreach($data as $fetch): ?>
            <tr>
    <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']?>">
                <td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
                <td class="text-center">
                    <button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update"> 
                        <span class = "glyphicon glyphicon-edit"></span> Update
                    </button> 
                    | 

                    <button class = "btn btn-success activate-btn action-btn" data-id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
                        <span class = "glyphicon glyphicon-check"></span> Activate
                    </button> 

                    <button style="display:none" class = "btn btn-danger deactivate-btn action-btn " data-id="<?= $fetch['id'] ?>"  data-action="deactivate">
                        <span class = "glyphicon glyphicon-folder-close"></span> deactivate
                    </button>
                </td>
            </tr>
        <?php endforeach; ?>

    </tbody>
</table>
     <form action="create.php" method="post" class="form-inline">
                <div class = "form-group">
                <label>Segment:</label>
                <input type  = "text" name = "segment" class = "form-control" required = "required"/>
            </div>
                <div class = "form-group">
                    <button type="submit" name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button>
            </div>
            </form>
    <script type="text/javascript">
        $(".action-btn").on("click", function(e) {
            var id      = $(this).attr("data-id");
            var segment = $(this).parents("tr").find("td.segment").html();
            var action  = $(this).attr("data-action");
            $.ajax({
                "url": "action.php",
                "method": "post",
                "data": {
                    "id":      id,
                    "segment": segment,
                    "action":  action
                },
                success: function(data) {
                    alert(data);
                }
            });
        });  </script>
    <script>$(".activate-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});</script>
</body>

问题是,当我点击任何激活按钮时,所有停用按钮都会显示,激活不会消失,

我的意思是,当我点击激活每个其他td的停用时,当我再次点击停用时,激活按钮不显示,有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

您可能想要更改

$(".activate-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});

成:

$(".action-btn").click(function(){$(this).hide();$(".deactivate-btn").show();});
$(".deactivate-btn").click(function(){$(this).show();$(".action-btn").show();});

答案 1 :(得分:0)

有两个按钮会让最终用户感到困惑,因为他们不知道哪个段处于活动状态以及哪个段已停用。从您的代码中,最初加载页面时会显示所有按钮。另一种选择是:

if($fetch['status']=="active"){
<button class="btn-danger statusbtn" data-action="deactivate"
    id="<?=$fetch['id']?>">
    <span class="glyphicon glyphicon-folder-close"></span>
    Deactivate
</button>
}else{
<button class="btn-success statusbtn" data-action="activate"
    id="<?=$fetch['id']?>">
    <span class="glyphicon glyphicon-ok"></span>
    Activate
</button>
}

然后编辑您的JavaScript以阅读:

  <script type="text/javascript">
            $(".action-btn").on("click", function(e) {
                var id      = $(this).attr("id");
                var segment =       $(this).parents("tr").find("td.segment").html();
                var action  = $(this).attr("data-action");
                $.ajax({
                    "url": "action.php",
                    "method": "post",
                    "data": {
                        "id":      id,
                        "segment": segment,
                        "action":  action
                    },
                    success: function(data) {
                      if(action=='activate'){
                        $(this).html("    
                          <span class='glyphicon glyphicon-folder-close'>      
                          </span>
    Deactivate");
    $(this).attr('data-action',"deactivate");
    $(this).removeClass('btn-success')
    $(this).addClass('btn-danger');
                      }else{
                       $(this).html("    
                          <span class='glyphicon glyphicon-ok'>      
                          </span>
    activate");
    $(this).attr('data-action',"activate");
    $(this).removeClass('btn-danger')
    $(this).addClass('btn-success');
                      }
                        alert(data);
                    }
                });
            });  
    </script>

我没有运行代码来测试错误,但我认为这可以解决您的问题。