jquery .show()。hide()不起作用

时间:2017-07-17 22:18:29

标签: javascript php jquery

您好我是javascript和jquery的新手。当我选择“编辑”时,我试图显示表单,但是我无法使其工作。任何帮助/建议表示赞赏。

php代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Manage Categories</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="test.js"></script>
</head>

<body>

<h1>Manage Categories</h1>
<h3>List of current categories: </h3>
<ul>
<table>

    <?php 
        $counter = 0;
        while($category = $categories->fetch_assoc()){
        if($category['name'] === "+ New Category"){
            continue;
        }
    ?>
        <tr>
            <td>
            <li>
                <?php echo $category['name']?>&nbsp;
                <a id="link<?php echo $counter ?>" href="manage_categories.php?type=edit&id=<?php echo $category['id'];?>">Edit</a>&nbsp;&nbsp; 
                <a href="manage_categories.php?type=delete&id=<?php echo $category['id'];?>">Delete</a>
            </li>
            </td>
        </tr>

<div id="edit_form<?php echo $counter ?>">
    <form action="manage_categories.php?type=edit&id=<?php echo $category['id'];?>" method="POST">

        <tr id="abc">
            <td>New category name:</td>
            <td><input type="text" name="new_cat" /></td>
            <td><input type="submit" name="submit" value="Change" /></td>

        </tr>

    </form>
  </div>
    <?php
    $counter++; 
    }

    ?>

    <form action="manage_categories.php?type=add" method="POST">
        <tr>
            <td><a href="manage_categories.php?type=add">Add New Category</a></td>
        </tr>

        <tr>
            <td>New category name: </td>
            <td><input type="text" name="new_cat" /></td>
            <td><input type="submit" name="submit" value="Add" /></td>
        </tr>
    </form>

</table>
</ul>

<a href="<?php echo $journal_url ?>">Return to journal homepage</a>
</body>
</html>

js file:

$(document).ready(function () {
    $("#edit_form1").hide();
    $("#link1").on("click", function(){
        alert("hello");
        if($("#edit_form1").is(":visible")){
                $("#edit_form1").hide();
            } else {
                $("#edit_form1").show();
            }
    });

    $("#link2").on("click", function(){
        alert("hello");
        $("#edit_form2").hide();
    });


});

alert()函数有效,但我无法隐藏/显示表单(默认显示)。 php脚本只返回我在数据库中的类别列表。

1 个答案:

答案 0 :(得分:0)

您的PHP代码中存在错误。首先修复那个bug。 你用过了

while($category = $categories->fetch_assoc());
这里
$categories
未定。因为你没有在$ categories变量中保留mysqli的对象。修复此