在单独的php文件上定义Bootstrap按钮

时间:2015-09-04 18:13:22

标签: php jquery html twitter-bootstrap-3

使用两个php文件,index.php和search.php。 index将一些参数发送到搜索,它会对数据库执行一些查询,然后返回表中的信息,存储到$ output中。 我现在想在输出中添加一个bootstrap按钮,它调用一个简单的show / hide jQuery函数。 按钮已创建,但在index.php

上测试时它不起作用
<div class="col-sm-8">
        <table class="table table-hover" id="output">
        </table>

        <div id="edit" >
          <div class="page-header" id="editar" style="display: none;">
            <h2 id="producto">Placeholder<button id="exit" type="button" class="btn pull-right"><span class="glyphicon glyphicon-remove"></button><!--Clicking this button hides the edit div and shows the output table--></h2>
          </div>
        </div>
  </div>

exit / editbtn按钮调用以下内容:

  <script>
    $(document).ready(function(){
       $("#exit").click(function(){
          $(document.getElementById("edit")).hide();
          $(document.getElementById("output")).show();
       });
    });
    $(document).ready(function(){
       $("#editbtn").click(function(){
          $(document.getElementById("edit")).show();
          $(document.getElementById("output")).hide();
       });
     });
</script>

&#34; editbtn&#34;的定义是在单独的php文件上进行的:

if($query->num_rows){
  $rows = $query->fetch_all(MYSQLI_ASSOC);
  forEach($rows as $row){
    $output .=  '<tr><td><button id="editbtn" type="button" class="btn btn-default"><span class="glyphicon glyphicon-pencil"></span></button></td><!--Clicking this button hides the output table and shows the edit div--></tr>'; 
  }
  $output .= '</tbody>';

所以最后,我创建了一个带有按钮的表,但是当我点击它时它什么也没做。

2 个答案:

答案 0 :(得分:1)

为什么不按顺序尝试?

像:

<script>
    $(document).ready(function(){
       $("#exit").click(function(){
          $("#edit").hide();
          $("#output").show();
       });

       $("#editbtn").click(function(){
          $("#edit")).show();
          $("#output")).hide();
       });
     });
</script>

这应该有用,总是你不要按照定义插入带有ajax AND 的编辑按钮,如果你使用的是ID,你应该只有一个元素,如果你有一个foreach ,它可能会给你带来麻烦。

答案 1 :(得分:0)

它似乎是jQuery选择器,它们应该是这样的:

$("#edit").show();

或者在JavaScript中:

document.getElementById('edit').styles.display = "none"; //or use addClass

PD:不要使用两个就绪函数,将侦听器放在同一个函数上;)

编辑:

在函数中添加console.log,并在浏览器的开发人员工具上查看控制台。还尝试注释PHP查询,输出除外。

相关问题