在输入onclick()上调用时未定义Javascript函数

时间:2015-07-14 19:17:00

标签: web

我一直在

  

ReferenceError:del未定义

在它工作之前它会刷新页面,但我将其设置为只在点击时刷新div。

while($row = $result->fetch_assoc()) {
            $fileID = $row['fileID'];
            echo "<tbody><tr>";
            echo "
                    <td bgcolor='white'>
                        <input type='checkbox' checked>
                    </td>
                    <td bgcolor='white'>
                        {$row['name']}
                    </td>
                    <td bgcolor='white'>
                        {$row['type']}
                    </td>
                    <td bgcolor='white'>
                        {$row['size']}
                    </td>
                    <td bgcolor='white'>
                        {$row['created']}
                    </td>

                    <td>
                        <input type='button' name='download' value='Download' onclick='window.location=&quot;get_file.php?fileID=$fileID&quot;;'>
                    </td>
                    <td>
                        <script type='text/javascript'>
                            function del() {
                                console.log('Im working!');
                                var r = confirm('Do you want to remove this file?');
                                if (r == true) {
                                    $.ajax({url: 'delete.php',
                                            data: {action: '<?php print $fileID;?>'}
                                            type: 'post';
                                            success: function() {
                                                $('#fileTable').load(document.URL + ' #fileTable');
                                            }
                                    });
                                }
                            }
                        </script>
                        <input type='button' name='delete' value='Delete' onclick='del()'>

                    </td>

1 个答案:

答案 0 :(得分:0)

您为每一行复制了您的功能,当您按下某个按钮时,该功能无法知道您之后的ID。

将其更改为:

echo <<<EOHTML

                        <script type='text/javascript'>
                            function del(id) {
                                console.log('Im working!', id);
                                var r = confirm('Do you want to remove this file?');
                                if (r == true) {
                                    $.ajax({url: 'delete.php',
                                            data: {action: id.toString()}
                                            type: 'post';
                                            success: function() {
                                                $('#fileTable').load(document.URL + ' #fileTable');
                                            }
                                    });
                                }
                            }
                        </script>

EOHTML;

while($row = $result->fetch_assoc()) {
            $fileID = $row['fileID'];
            echo <<<EOHTML
                <tbody><tr>
                    <td bgcolor='white'>
                        <input type='checkbox' checked>
                    </td>
                    <td bgcolor='white'>
                        {$row['name']}
                    </td>
                    <td bgcolor='white'>
                        {$row['type']}
                    </td>
                    <td bgcolor='white'>
                        {$row['size']}
                    </td>
                    <td bgcolor='white'>
                        {$row['created']}
                    </td>

                    <td>
                        <input type='button' name='download' value='Download' onclick='window.location=&quot;get_file.php?fileID={$fileID}&quot;;'>
                    </td>
                    <td>
                        <input type='button' name='delete' value='Delete' onclick='del({$fileID})'>
                    </td>
EOHTML;
}